home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / simple.el.z / simple.el
Lisp/Scheme  |  1998-10-27  |  118KB  |  3,136 lines

  1. ;;; simple.el --- basic editing commands for Emacs
  2.  
  3. ;; Copyright (C) 1985, 86, 87, 93, 94, 95 Free Software Foundation, Inc.
  4.  
  5. ;; This file is part of GNU Emacs.
  6.  
  7. ;; GNU Emacs is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 2, or (at your option)
  10. ;; any later version.
  11.  
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ;; GNU General Public License for more details.
  16.  
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  19. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. ;; Boston, MA 02111-1307, USA.
  21.  
  22. ;;; Commentary:
  23.  
  24. ;; A grab-bag of basic Emacs commands not specifically related to some
  25. ;; major mode or to file-handling.
  26.  
  27. ;;; Code:
  28.  
  29. (defun newline (&optional arg)
  30.   "Insert a newline, and move to left margin of the new line if it's blank.
  31. The newline is marked with the text-property `hard'.
  32. With arg, insert that many newlines.
  33. In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
  34.   (interactive "*P")
  35.   (barf-if-buffer-read-only)
  36.   ;; Inserting a newline at the end of a line produces better redisplay in
  37.   ;; try_window_id than inserting at the beginning of a line, and the textual
  38.   ;; result is the same.  So, if we're at beginning of line, pretend to be at
  39.   ;; the end of the previous line.
  40.   (let ((flag (and (not (bobp)) 
  41.            (bolp)
  42.            ;; Make sure the newline before point isn't intangible.
  43.            (not (get-char-property (1- (point)) 'intangible))
  44.            ;; Make sure the newline before point isn't read-only.
  45.            (not (get-char-property (1- (point)) 'read-only))
  46.            ;; Make sure the newline before point isn't invisible.
  47.            (not (get-char-property (1- (point)) 'invisible))
  48.            ;; Make sure the newline before point has the same
  49.            ;; properties as the char before it (if any).
  50.            (< (or (previous-property-change (point)) -2) 
  51.               (- (point) 2))))
  52.     (was-page-start (and (bolp)
  53.                  (looking-at page-delimiter)))
  54.     (beforepos (point)))
  55.     (if flag (backward-char 1))
  56.     ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
  57.     ;; Set last-command-char to tell self-insert what to insert.
  58.     (let ((last-command-char ?\n)
  59.       ;; Don't auto-fill if we have a numeric argument.
  60.       ;; Also not if flag is true (it would fill wrong line);
  61.       ;; there is no need to since we're at BOL.
  62.       (auto-fill-function (if (or arg flag) nil auto-fill-function)))
  63.       (unwind-protect
  64.       (self-insert-command (prefix-numeric-value arg))
  65.     ;; If we get an error in self-insert-command, put point at right place.
  66.     (if flag (forward-char 1))))
  67.     ;; If we did *not* get an error, cancel that forward-char.
  68.     (if flag (backward-char 1))
  69.     ;; Mark the newline(s) `hard'.
  70.     (if use-hard-newlines
  71.     (let* ((from (- (point) (if arg (prefix-numeric-value arg) 1)))
  72.            (sticky (get-text-property from 'rear-nonsticky)))
  73.       (put-text-property from (point) 'hard 't)
  74.       ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
  75.       (if (and (listp sticky) (not (memq 'hard sticky)))
  76.           (put-text-property from (point) 'rear-nonsticky
  77.                  (cons 'hard sticky)))))
  78.     ;; If the newline leaves the previous line blank,
  79.     ;; and we have a left margin, delete that from the blank line.
  80.     (or flag
  81.     (save-excursion
  82.       (goto-char beforepos)
  83.       (beginning-of-line)
  84.       (and (looking-at "[ \t]$")
  85.            (> (current-left-margin) 0)
  86.            (delete-region (point) (progn (end-of-line) (point))))))
  87.     (if flag (forward-char 1))
  88.     ;; Indent the line after the newline, except in one case:
  89.     ;; when we added the newline at the beginning of a line
  90.     ;; which starts a page.
  91.     (or was-page-start
  92.     (move-to-left-margin nil t)))
  93.   nil)
  94.  
  95. (defun open-line (arg)
  96.   "Insert a newline and leave point before it.
  97. If there is a fill prefix and/or a left-margin, insert them on the new line
  98. if the line would have been blank.
  99. With arg N, insert N newlines."
  100.   (interactive "*p")
  101.   (let* ((do-fill-prefix (and fill-prefix (bolp)))
  102.      (do-left-margin (and (bolp) (> (current-left-margin) 0)))
  103.      (loc (point)))
  104.     (newline arg)
  105.     (goto-char loc)
  106.     (while (> arg 0)
  107.       (cond ((bolp)
  108.          (if do-left-margin (indent-to (current-left-margin)))
  109.          (if do-fill-prefix (insert-and-inherit fill-prefix))))
  110.       (forward-line 1)
  111.       (setq arg (1- arg)))
  112.     (goto-char loc)
  113.     (end-of-line)))
  114.  
  115. (defun split-line ()
  116.   "Split current line, moving portion beyond point vertically down."
  117.   (interactive "*")
  118.   (skip-chars-forward " \t")
  119.   (let ((col (current-column))
  120.     (pos (point)))
  121.     (newline 1)
  122.     (indent-to col 0)
  123.     (goto-char pos)))
  124.  
  125. (defun quoted-insert (arg)
  126.   "Read next input character and insert it.
  127. This is useful for inserting control characters.
  128. You may also type up to 3 octal digits, to insert a character with that code.
  129.  
  130. In overwrite mode, this function inserts the character anyway, and
  131. does not handle octal digits specially.  This means that if you use
  132. overwrite as your normal editing mode, you can use this function to
  133. insert characters when necessary.
  134.  
  135. In binary overwrite mode, this function does overwrite, and octal
  136. digits are interpreted as a character code.  This is supposed to make
  137. this function useful in editing binary files."
  138.   (interactive "*p")
  139.   (let ((char (if (or (not overwrite-mode)
  140.               (eq overwrite-mode 'overwrite-mode-binary))
  141.           (read-quoted-char)
  142.         (read-char))))
  143.     (if (> arg 0)
  144.     (if (eq overwrite-mode 'overwrite-mode-binary)
  145.         (delete-char arg)))
  146.     (while (> arg 0)
  147.       (insert-and-inherit char)
  148.       (setq arg (1- arg)))))
  149.  
  150. (defun delete-indentation (&optional arg)
  151.   "Join this line to previous and fix up whitespace at join.
  152. If there is a fill prefix, delete it from the beginning of this line.
  153. With argument, join this line to following line."
  154.   (interactive "*P")
  155.   (beginning-of-line)
  156.   (if arg (forward-line 1))
  157.   (if (eq (preceding-char) ?\n)
  158.       (progn
  159.     (delete-region (point) (1- (point)))
  160.     ;; If the second line started with the fill prefix,
  161.     ;; delete the prefix.
  162.     (if (and fill-prefix
  163.          (<= (+ (point) (length fill-prefix)) (point-max))
  164.          (string= fill-prefix
  165.               (buffer-substring (point)
  166.                         (+ (point) (length fill-prefix)))))
  167.         (delete-region (point) (+ (point) (length fill-prefix))))
  168.     (fixup-whitespace))))
  169.  
  170. (defun fixup-whitespace ()
  171.   "Fixup white space between objects around point.
  172. Leave one space or none, according to the context."
  173.   (interactive "*")
  174.   (save-excursion
  175.     (delete-horizontal-space)
  176.     (if (or (looking-at "^\\|\\s)")
  177.         (save-excursion (forward-char -1)
  178.                 (looking-at "$\\|\\s(\\|\\s'")))
  179.     nil
  180.       (insert ?\ ))))
  181.  
  182. (defun delete-horizontal-space ()
  183.   "Delete all spaces and tabs around point."
  184.   (interactive "*")
  185.   (skip-chars-backward " \t")
  186.   (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  187.  
  188. (defun just-one-space ()
  189.   "Delete all spaces and tabs around point, leaving one space."
  190.   (interactive "*")
  191.   (skip-chars-backward " \t")
  192.   (if (= (following-char) ? )
  193.       (forward-char 1)
  194.     (insert ? ))
  195.   (delete-region (point) (progn (skip-chars-forward " \t") (point))))
  196.  
  197. (defun delete-blank-lines ()
  198.   "On blank line, delete all surrounding blank lines, leaving just one.
  199. On isolated blank line, delete that one.
  200. On nonblank line, delete any immediately following blank lines."
  201.   (interactive "*")
  202.   (let (thisblank singleblank)
  203.     (save-excursion
  204.       (beginning-of-line)
  205.       (setq thisblank (looking-at "[ \t]*$"))
  206.       ;; Set singleblank if there is just one blank line here.
  207.       (setq singleblank
  208.         (and thisblank
  209.          (not (looking-at "[ \t]*\n[ \t]*$"))
  210.          (or (bobp)
  211.              (progn (forward-line -1)
  212.                 (not (looking-at "[ \t]*$")))))))
  213.     ;; Delete preceding blank lines, and this one too if it's the only one.
  214.     (if thisblank
  215.     (progn
  216.       (beginning-of-line)
  217.       (if singleblank (forward-line 1))
  218.       (delete-region (point)
  219.              (if (re-search-backward "[^ \t\n]" nil t)
  220.                  (progn (forward-line 1) (point))
  221.                (point-min)))))
  222.     ;; Delete following blank lines, unless the current line is blank
  223.     ;; and there are no following blank lines.
  224.     (if (not (and thisblank singleblank))
  225.     (save-excursion
  226.       (end-of-line)
  227.       (forward-line 1)
  228.       (delete-region (point)
  229.              (if (re-search-forward "[^ \t\n]" nil t)
  230.                  (progn (beginning-of-line) (point))
  231.                (point-max)))))
  232.     ;; Handle the special case where point is followed by newline and eob.
  233.     ;; Delete the line, leaving point at eob.
  234.     (if (looking-at "^[ \t]*\n\\'")
  235.     (delete-region (point) (point-max)))))
  236.  
  237. (defun back-to-indentation ()
  238.   "Move point to the first non-whitespace character on this line."
  239.   (interactive)
  240.   (beginning-of-line 1)
  241.   (skip-chars-forward " \t"))
  242.  
  243. (defun newline-and-indent ()
  244.   "Insert a newline, then indent according to major mode.
  245. Indentation is done using the value of `indent-line-function'.
  246. In programming language modes, this is the same as TAB.
  247. In some text modes, where TAB inserts a tab, this command indents to the
  248. column specified by the function `current-left-margin'."
  249.   (interactive "*")
  250.   (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  251.   (newline)
  252.   (indent-according-to-mode))
  253.  
  254. (defun reindent-then-newline-and-indent ()
  255.   "Reindent current line, insert newline, then indent the new line.
  256. Indentation of both lines is done according to the current major mode,
  257. which means calling the current value of `indent-line-function'.
  258. In programming language modes, this is the same as TAB.
  259. In some text modes, where TAB inserts a tab, this indents to the
  260. column specified by the function `current-left-margin'."
  261.   (interactive "*")
  262.   (save-excursion
  263.     (delete-region (point) (progn (skip-chars-backward " \t") (point)))
  264.     (indent-according-to-mode))
  265.   (newline)
  266.   (indent-according-to-mode))
  267.  
  268. ;; Internal subroutine of delete-char
  269. (defun kill-forward-chars (arg)
  270.   (if (listp arg) (setq arg (car arg)))
  271.   (if (eq arg '-) (setq arg -1))
  272.   (kill-region (point) (+ (point) arg)))
  273.  
  274. ;; Internal subroutine of backward-delete-char
  275. (defun kill-backward-chars (arg)
  276.   (if (listp arg) (setq arg (car arg)))
  277.   (if (eq arg '-) (setq arg -1))
  278.   (kill-region (point) (- (point) arg)))
  279.  
  280. (defun backward-delete-char-untabify (arg &optional killp)
  281.   "Delete characters backward, changing tabs into spaces.
  282. Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
  283. Interactively, ARG is the prefix arg (default 1)
  284. and KILLP is t if a prefix arg was specified."
  285.   (interactive "*p\nP")
  286.   (let ((count arg))
  287.     (save-excursion
  288.       (while (and (> count 0) (not (bobp)))
  289.     (if (= (preceding-char) ?\t)
  290.         (let ((col (current-column)))
  291.           (forward-char -1)
  292.           (setq col (- col (current-column)))
  293.           (insert-char ?\ col)
  294.           (delete-char 1)))
  295.     (forward-char -1)
  296.     (setq count (1- count)))))
  297.   (delete-backward-char arg killp))
  298.  
  299. (defun zap-to-char (arg char)
  300.   "Kill up to and including ARG'th occurrence of CHAR.
  301. Goes backward if ARG is negative; error if CHAR not found."
  302.   (interactive "p\ncZap to char: ")
  303.   (kill-region (point) (progn
  304.              (search-forward (char-to-string char) nil nil arg)
  305. ;             (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
  306.              (point))))
  307.  
  308. (defun beginning-of-buffer (&optional arg)
  309.   "Move point to the beginning of the buffer; leave mark at previous position.
  310. With arg N, put point N/10 of the way from the beginning.
  311.  
  312. If the buffer is narrowed, this command uses the beginning and size
  313. of the accessible part of the buffer.
  314.  
  315. Don't use this command in Lisp programs!
  316. \(goto-char (point-min)) is faster and avoids clobbering the mark."
  317.   (interactive "P")
  318.   (push-mark)
  319.   (let ((size (- (point-max) (point-min))))
  320.     (goto-char (if arg
  321.            (+ (point-min)
  322.               (if (> size 10000)
  323.               ;; Avoid overflow for large buffer sizes!
  324.               (* (prefix-numeric-value arg)
  325.                  (/ size 10))
  326.             (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
  327.          (point-min))))
  328.   (if arg (forward-line 1)))
  329.  
  330. (defun end-of-buffer (&optional arg)
  331.   "Move point to the end of the buffer; leave mark at previous position.
  332. With arg N, put point N/10 of the way from the end.
  333.  
  334. If the buffer is narrowed, this command uses the beginning and size
  335. of the accessible part of the buffer.
  336.  
  337. Don't use this command in Lisp programs!
  338. \(goto-char (point-max)) is faster and avoids clobbering the mark."
  339.   (interactive "P")
  340.   (push-mark)
  341.   (let ((size (- (point-max) (point-min))))
  342.     (goto-char (if arg
  343.            (- (point-max)
  344.               (if (> size 10000)
  345.               ;; Avoid overflow for large buffer sizes!
  346.               (* (prefix-numeric-value arg)
  347.                  (/ size 10))
  348.             (/ (* size (prefix-numeric-value arg)) 10)))
  349.          (point-max))))
  350.   ;; If we went to a place in the middle of the buffer,
  351.   ;; adjust it to the beginning of a line.
  352.   (if arg (forward-line 1)
  353.     ;; If the end of the buffer is not already on the screen,
  354.     ;; then scroll specially to put it near, but not at, the bottom.
  355.     (if (let ((old-point (point)))
  356.       (save-excursion
  357.             (goto-char (window-start))
  358.             (vertical-motion (window-height))
  359.             (< (point) old-point)))
  360.     (progn
  361.       (overlay-recenter (point))
  362.       (recenter -3)))))
  363.  
  364. (defun mark-whole-buffer ()
  365.   "Put point at beginning and mark at end of buffer.
  366. You probably should not use this function in Lisp programs;
  367. it is usually a mistake for a Lisp function to use any subroutine
  368. that uses or sets the mark."
  369.   (interactive)
  370.   (push-mark (point))
  371.   (push-mark (point-max) nil t)
  372.   (goto-char (point-min)))
  373.  
  374. (defun count-lines-region (start end)
  375.   "Print number of lines and characters in the region."
  376.   (interactive "r")
  377.   (message "Region has %d lines, %d characters"
  378.        (count-lines start end) (- end start)))
  379.  
  380. (defun what-line ()
  381.   "Print the current buffer line number and narrowed line number of point."
  382.   (interactive)
  383.   (let ((opoint (point)) start)
  384.     (save-excursion
  385.       (save-restriction
  386.     (goto-char (point-min))
  387.     (widen)
  388.     (beginning-of-line)
  389.     (setq start (point))
  390.     (goto-char opoint)
  391.     (beginning-of-line)
  392.     (if (/= start 1)
  393.         (message "line %d (narrowed line %d)"
  394.              (1+ (count-lines 1 (point)))
  395.              (1+ (count-lines start (point))))
  396.       (message "Line %d" (1+ (count-lines 1 (point)))))))))
  397.  
  398.  
  399. (defun count-lines (start end)
  400.   "Return number of lines between START and END.
  401. This is usually the number of newlines between them,
  402. but can be one more if START is not equal to END
  403. and the greater of them is not at the start of a line."
  404.   (save-excursion
  405.     (save-restriction
  406.       (narrow-to-region start end)
  407.       (goto-char (point-min))
  408.       (if (eq selective-display t)
  409.       (save-match-data
  410.         (let ((done 0))
  411.           (while (re-search-forward "[\n\C-m]" nil t 40)
  412.         (setq done (+ 40 done)))
  413.           (while (re-search-forward "[\n\C-m]" nil t 1)
  414.         (setq done (+ 1 done)))
  415.           (goto-char (point-max))
  416.           (if (and (/= start end)
  417.                (not (bolp)))
  418.           (1+ done)
  419.         done)))
  420.     (- (buffer-size) (forward-line (buffer-size)))))))
  421.  
  422. (defun what-cursor-position ()
  423.   "Print info on cursor position (on screen and within buffer)."
  424.   (interactive)
  425.   (let* ((char (following-char))
  426.      (beg (point-min))
  427.      (end (point-max))
  428.          (pos (point))
  429.      (total (buffer-size))
  430.      (percent (if (> total 50000)
  431.               ;; Avoid overflow from multiplying by 100!
  432.               (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
  433.             (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
  434.      (hscroll (if (= (window-hscroll) 0)
  435.               ""
  436.             (format " Hscroll=%d" (window-hscroll))))
  437.      (col (current-column)))
  438.     (if (= pos end)
  439.     (if (or (/= beg 1) (/= end (1+ total)))
  440.         (message "point=%d of %d(%d%%) <%d - %d>  column %d %s"
  441.              pos total percent beg end col hscroll)
  442.       (message "point=%d of %d(%d%%)  column %d %s"
  443.            pos total percent col hscroll))
  444.       (if (or (/= beg 1) (/= end (1+ total)))
  445.       (message "Char: %s (0%o, %d, 0x%x)  point=%d of %d(%d%%) <%d - %d>  column %d %s"
  446.            (single-key-description char) char char char pos total percent beg end col hscroll)
  447.     (message "Char: %s (0%o, %d, 0x%x)  point=%d of %d(%d%%)  column %d %s"
  448.          (single-key-description char) char char char pos total percent col hscroll)))))
  449.  
  450. (defun fundamental-mode ()
  451.   "Major mode not specialized for anything in particular.
  452. Other major modes are defined by comparison with this one."
  453.   (interactive)
  454.   (kill-all-local-variables))
  455.  
  456. (defvar read-expression-map (cons 'keymap minibuffer-local-map)
  457.   "Minibuffer keymap used for reading Lisp expressions.")
  458. (define-key read-expression-map "\M-\t" 'lisp-complete-symbol)
  459.  
  460. (put 'eval-expression 'disabled t)
  461.  
  462. (defvar read-expression-history nil)
  463.  
  464. ;; We define this, rather than making `eval' interactive,
  465. ;; for the sake of completion of names like eval-region, eval-current-buffer.
  466. (defun eval-expression (expression)
  467.   "Evaluate EXPRESSION and print value in minibuffer.
  468. Value is also consed on to front of the variable `values'."
  469.   (interactive
  470.    (list (read-from-minibuffer "Eval: "
  471.                    nil read-expression-map t
  472.                    'read-expression-history)))
  473.   (setq values (cons (eval expression) values))
  474.   (prin1 (car values) t))
  475.  
  476. (defun edit-and-eval-command (prompt command)
  477.   "Prompting with PROMPT, let user edit COMMAND and eval result.
  478. COMMAND is a Lisp expression.  Let user edit that expression in
  479. the minibuffer, then read and evaluate the result."
  480.   (let ((command (read-from-minibuffer prompt
  481.                        (prin1-to-string command)
  482.                        read-expression-map t
  483.                        '(command-history . 1))))
  484.     ;; If command was added to command-history as a string,
  485.     ;; get rid of that.  We want only evaluable expressions there.
  486.     (if (stringp (car command-history))
  487.     (setq command-history (cdr command-history)))
  488.  
  489.     ;; If command to be redone does not match front of history,
  490.     ;; add it to the history.
  491.     (or (equal command (car command-history))
  492.     (setq command-history (cons command command-history)))
  493.     (eval command)))
  494.  
  495. (defun repeat-complex-command (arg)
  496.   "Edit and re-evaluate last complex command, or ARGth from last.
  497. A complex command is one which used the minibuffer.
  498. The command is placed in the minibuffer as a Lisp form for editing.
  499. The result is executed, repeating the command as changed.
  500. If the command has been changed or is not the most recent previous command
  501. it is added to the front of the command history.
  502. You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
  503. to get different commands to edit and resubmit."
  504.   (interactive "p")
  505.   (let ((elt (nth (1- arg) command-history))
  506.     (minibuffer-history-position arg)
  507.     (minibuffer-history-sexp-flag t)
  508.     newcmd)
  509.     (if elt
  510.     (progn
  511.       (setq newcmd
  512.         (let ((print-level nil))
  513.           (read-from-minibuffer
  514.            "Redo: " (prin1-to-string elt) read-expression-map t
  515.            (cons 'command-history arg))))
  516.  
  517.       ;; If command was added to command-history as a string,
  518.       ;; get rid of that.  We want only evaluable expressions there.
  519.       (if (stringp (car command-history))
  520.           (setq command-history (cdr command-history)))
  521.  
  522.       ;; If command to be redone does not match front of history,
  523.       ;; add it to the history.
  524.       (or (equal newcmd (car command-history))
  525.           (setq command-history (cons newcmd command-history)))
  526.       (eval newcmd))
  527.       (ding))))
  528.  
  529. (defvar minibuffer-history nil
  530.   "Default minibuffer history list.
  531. This is used for all minibuffer input
  532. except when an alternate history list is specified.")
  533. (defvar minibuffer-history-sexp-flag nil
  534.   "Non-nil when doing history operations on `command-history'.
  535. More generally, indicates that the history list being acted on
  536. contains expressions rather than strings.")
  537. (setq minibuffer-history-variable 'minibuffer-history)
  538. (setq minibuffer-history-position nil)
  539. (defvar minibuffer-history-search-history nil)
  540.  
  541. (mapcar
  542.  (lambda (key-and-command)
  543.    (mapcar
  544.     (lambda (keymap-and-completionp)
  545.       ;; Arg is (KEYMAP-SYMBOL . COMPLETION-MAP-P).
  546.       ;; If the cdr of KEY-AND-COMMAND (the command) is a cons,
  547.       ;; its car is used if COMPLETION-MAP-P is nil, its cdr if it is t.
  548.       (define-key (symbol-value (car keymap-and-completionp))
  549.     (car key-and-command)
  550.     (let ((command (cdr key-and-command)))
  551.       (if (consp command)
  552.           ;; (and ... nil) => ... turns back on the completion-oriented
  553.           ;; history commands which rms turned off since they seem to
  554.           ;; do things he doesn't like.
  555.           (if (and (cdr keymap-and-completionp) nil) ;XXX turned off
  556.           (progn (error "EMACS BUG!") (cdr command))
  557.         (car command))
  558.         command))))
  559.     '((minibuffer-local-map . nil)
  560.       (minibuffer-local-ns-map . nil)
  561.       (minibuffer-local-completion-map . t)
  562.       (minibuffer-local-must-match-map . t)
  563.       (read-expression-map . nil))))
  564.  '(("\en" . (next-history-element . next-complete-history-element))
  565.    ([next] . (next-history-element . next-complete-history-element))
  566.    ("\ep" . (previous-history-element . previous-complete-history-element))
  567.    ([prior] . (previous-history-element . previous-complete-history-element))
  568.    ("\er" . previous-matching-history-element)
  569.    ("\es" . next-matching-history-element)))
  570.  
  571. (defun previous-matching-history-element (regexp n)
  572.   "Find the previous history element that matches REGEXP.
  573. \(Previous history elements refer to earlier actions.)
  574. With prefix argument N, search for Nth previous match.
  575. If N is negative, find the next or Nth next match."
  576.   (interactive
  577.    (let* ((enable-recursive-minibuffers t)
  578.       (minibuffer-history-sexp-flag nil)
  579.       (regexp (read-from-minibuffer "Previous element matching (regexp): "
  580.                     nil
  581.                     minibuffer-local-map
  582.                     nil
  583.                     'minibuffer-history-search-history)))
  584.      ;; Use the last regexp specified, by default, if input is empty.
  585.      (list (if (string= regexp "")
  586.            (if minibuffer-history-search-history
  587.            (car minibuffer-history-search-history)
  588.          (error "No previous history search regexp"))
  589.          regexp)
  590.        (prefix-numeric-value current-prefix-arg))))
  591.   (let ((history (symbol-value minibuffer-history-variable))
  592.     prevpos
  593.     (pos minibuffer-history-position))
  594.     (while (/= n 0)
  595.       (setq prevpos pos)
  596.       (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history)))
  597.       (if (= pos prevpos)
  598.       (error (if (= pos 1)
  599.              "No later matching history item"
  600.            "No earlier matching history item")))
  601.       (if (string-match regexp
  602.             (if minibuffer-history-sexp-flag
  603.                 (let ((print-level nil))
  604.                   (prin1-to-string (nth (1- pos) history)))
  605.               (nth (1- pos) history)))
  606.       (setq n (+ n (if (< n 0) 1 -1)))))
  607.     (setq minibuffer-history-position pos)
  608.     (erase-buffer)
  609.     (let ((elt (nth (1- pos) history)))
  610.       (insert (if minibuffer-history-sexp-flag
  611.           (let ((print-level nil))
  612.             (prin1-to-string elt))
  613.         elt)))
  614.       (goto-char (point-min)))
  615.   (if (or (eq (car (car command-history)) 'previous-matching-history-element)
  616.       (eq (car (car command-history)) 'next-matching-history-element))
  617.       (setq command-history (cdr command-history))))
  618.  
  619. (defun next-matching-history-element (regexp n)
  620.   "Find the next history element that matches REGEXP.
  621. \(The next history element refers to a more recent action.)
  622. With prefix argument N, search for Nth next match.
  623. If N is negative, find the previous or Nth previous match."
  624.   (interactive
  625.    (let* ((enable-recursive-minibuffers t)
  626.       (minibuffer-history-sexp-flag nil)
  627.       (regexp (read-from-minibuffer "Next element matching (regexp): "
  628.                     nil
  629.                     minibuffer-local-map
  630.                     nil
  631.                     'minibuffer-history-search-history)))
  632.      ;; Use the last regexp specified, by default, if input is empty.
  633.      (list (if (string= regexp "")
  634.            (setcar minibuffer-history-search-history
  635.                (nth 1 minibuffer-history-search-history))
  636.          regexp)
  637.        (prefix-numeric-value current-prefix-arg))))
  638.   (previous-matching-history-element regexp (- n)))
  639.  
  640. (defun next-history-element (n)
  641.   "Insert the next element of the minibuffer history into the minibuffer."
  642.   (interactive "p")
  643.   (or (zerop n)
  644.       (let ((narg (min (max 1 (- minibuffer-history-position n))
  645.                (length (symbol-value minibuffer-history-variable)))))
  646.         (if (or (zerop narg)
  647.             (= minibuffer-history-position narg))
  648.         (error (if (if (zerop narg)
  649.                    (> n 0)
  650.                  (= minibuffer-history-position 1))
  651.                "End of history; no next item"
  652.              "Beginning of history; no preceding item"))
  653.       (erase-buffer)
  654.       (setq minibuffer-history-position narg)
  655.       (let ((elt (nth (1- minibuffer-history-position)
  656.               (symbol-value minibuffer-history-variable))))
  657.         (insert
  658.          (if minibuffer-history-sexp-flag
  659.          (let ((print-level nil))
  660.            (prin1-to-string elt))
  661.            elt)))
  662.       (goto-char (point-min))))))
  663.  
  664. (defun previous-history-element (n)
  665.   "Inserts the previous element of the minibuffer history into the minibuffer."
  666.   (interactive "p")
  667.   (next-history-element (- n)))
  668.  
  669. (defun next-complete-history-element (n)
  670.   "Get next element of history which is a completion of minibuffer contents."
  671.   (interactive "p")
  672.   (let ((point-at-start (point)))
  673.     (next-matching-history-element
  674.      (concat "^" (regexp-quote (buffer-substring (point-min) (point)))) n)
  675.     ;; next-matching-history-element always puts us at (point-min).
  676.     ;; Move to the position we were at before changing the buffer contents.
  677.     ;; This is still sensical, because the text before point has not changed.
  678.     (goto-char point-at-start)))
  679.  
  680. (defun previous-complete-history-element (n)
  681.   "\
  682. Get previous element of history which is a completion of minibuffer contents."
  683.   (interactive "p")
  684.   (next-complete-history-element (- n)))
  685.  
  686. (defun goto-line (arg)
  687.   "Goto line ARG, counting from line 1 at beginning of buffer."
  688.   (interactive "NGoto line: ")
  689.   (setq arg (prefix-numeric-value arg))
  690.   (save-restriction
  691.     (widen)
  692.     (goto-char 1)
  693.     (if (eq selective-display t)
  694.     (re-search-forward "[\n\C-m]" nil 'end (1- arg))
  695.       (forward-line (1- arg)))))
  696.  
  697. ;Put this on C-x u, so we can force that rather than C-_ into startup msg
  698. (define-function 'advertised-undo 'undo)
  699.  
  700. (defun undo (&optional arg)
  701.   "Undo some previous changes.
  702. Repeat this command to undo more changes.
  703. A numeric argument serves as a repeat count."
  704.   (interactive "*p")
  705.   ;; If we don't get all the way thru, make last-command indicate that
  706.   ;; for the following command.
  707.   (setq this-command t)
  708.   (let ((modified (buffer-modified-p))
  709.     (recent-save (recent-auto-save-p)))
  710.     (or (eq (selected-window) (minibuffer-window))
  711.     (message "Undo!"))
  712.     (or (eq last-command 'undo)
  713.     (progn (undo-start)
  714.            (undo-more 1)))
  715.     (undo-more (or arg 1))
  716.     ;; Don't specify a position in the undo record for the undo command.
  717.     ;; Instead, undoing this should move point to where the change is.
  718.     (let ((tail buffer-undo-list)
  719.       done)
  720.       (while (and tail (not done) (not (null (car tail))))
  721.     (if (integerp (car tail))
  722.         (progn
  723.           (setq done t)
  724.           (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
  725.     (setq tail (cdr tail))))
  726.     (and modified (not (buffer-modified-p))
  727.      (delete-auto-save-file-if-necessary recent-save)))
  728.   ;; If we do get all the way thru, make this-command indicate that.
  729.   (setq this-command 'undo))
  730.  
  731. (defvar pending-undo-list nil
  732.   "Within a run of consecutive undo commands, list remaining to be undone.")
  733.  
  734. (defun undo-start ()
  735.   "Set `pending-undo-list' to the front of the undo list.
  736. The next call to `undo-more' will undo the most recently made change."
  737.   (if (eq buffer-undo-list t)
  738.       (error "No undo information in this buffer"))
  739.   (setq pending-undo-list buffer-undo-list))
  740.  
  741. (defun undo-more (count)
  742.   "Undo back N undo-boundaries beyond what was already undone recently.
  743. Call `undo-start' to get ready to undo recent changes,
  744. then call `undo-more' one or more times to undo them."
  745.   (or pending-undo-list
  746.       (error "No further undo information"))
  747.   (setq pending-undo-list (primitive-undo count pending-undo-list)))
  748.  
  749. (defvar shell-command-history nil
  750.   "History list for some commands that read shell commands.")
  751.  
  752. (defvar shell-command-switch "-c"
  753.   "Switch used to have the shell execute its command line argument.")
  754.  
  755. (defun shell-command (command &optional output-buffer)
  756.   "Execute string COMMAND in inferior shell; display output, if any.
  757.  
  758. If COMMAND ends in ampersand, execute it asynchronously.
  759. The output appears in the buffer `*Async Shell Command*'.
  760. That buffer is in shell mode.
  761.  
  762. Otherwise, COMMAND is executed synchronously.  The output appears in the
  763. buffer `*Shell Command Output*'.
  764. If the output is one line, it is displayed in the echo area *as well*,
  765. but it is nonetheless available in buffer `*Shell Command Output*',
  766. even though that buffer is not automatically displayed.
  767. If there is no output, or if output is inserted in the current buffer,
  768. then `*Shell Command Output*' is deleted.
  769.  
  770. The optional second argument OUTPUT-BUFFER, if non-nil,
  771. says to put the output in some other buffer.
  772. If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
  773. If OUTPUT-BUFFER is not a buffer and not nil,
  774. insert output in current buffer.  (This cannot be done asynchronously.)
  775. In either case, the output is inserted after point (leaving mark after it)."
  776.   (interactive (list (read-from-minibuffer "Shell command: "
  777.                        nil nil nil 'shell-command-history)
  778.              current-prefix-arg))
  779.   ;; Look for a handler in case default-directory is a remote file name.
  780.   (let ((handler
  781.      (find-file-name-handler (directory-file-name default-directory)
  782.                  'shell-command)))
  783.     (if handler
  784.     (funcall handler 'shell-command command output-buffer)
  785.       (if (and output-buffer
  786.            (not (or (bufferp output-buffer)  (stringp output-buffer))))
  787.       (progn (barf-if-buffer-read-only)
  788.          (push-mark)
  789.          ;; We do not use -f for csh; we will not support broken use of
  790.          ;; .cshrcs.  Even the BSD csh manual says to use
  791.          ;; "if ($?prompt) exit" before things which are not useful
  792.          ;; non-interactively.  Besides, if someone wants their other
  793.          ;; aliases for shell commands then they can still have them.
  794.          (call-process shell-file-name nil t nil
  795.                    shell-command-switch command)
  796.          ;; This is like exchange-point-and-mark, but doesn't
  797.          ;; activate the mark.  It is cleaner to avoid activation,
  798.          ;; even though the command loop would deactivate the mark
  799.          ;; because we inserted text.
  800.          (goto-char (prog1 (mark t)
  801.                   (set-marker (mark-marker) (point)
  802.                       (current-buffer)))))
  803.     ;; Preserve the match data in case called from a program.
  804.     (save-match-data
  805.       (if (string-match "[ \t]*&[ \t]*$" command)
  806.           ;; Command ending with ampersand means asynchronous.
  807.           (let ((buffer (get-buffer-create
  808.                  (or output-buffer "*Async Shell Command*")))
  809.             (directory default-directory)
  810.             proc)
  811.         ;; Remove the ampersand.
  812.         (setq command (substring command 0 (match-beginning 0)))
  813.         ;; If will kill a process, query first.
  814.         (setq proc (get-buffer-process buffer))
  815.         (if proc
  816.             (if (yes-or-no-p "A command is running.  Kill it? ")
  817.             (kill-process proc)
  818.               (error "Shell command in progress")))
  819.         (save-excursion
  820.           (set-buffer buffer)
  821.           (setq buffer-read-only nil)
  822.           (erase-buffer)
  823.           (display-buffer buffer)
  824.           (setq default-directory directory)
  825.           (setq proc (start-process "Shell" buffer shell-file-name 
  826.                         shell-command-switch command))
  827.           (setq mode-line-process '(":%s"))
  828.           (require 'shell) (shell-mode)
  829.           (set-process-sentinel proc 'shell-command-sentinel)
  830.           ))
  831.         (shell-command-on-region (point) (point) command nil)
  832.         ))))))
  833.  
  834. ;; We have a sentinel to prevent insertion of a termination message
  835. ;; in the buffer itself.
  836. (defun shell-command-sentinel (process signal)
  837.   (if (memq (process-status process) '(exit signal))
  838.       (message "%s: %s." 
  839.            (car (cdr (cdr (process-command process))))
  840.            (substring signal 0 -1))))
  841.  
  842. (defun shell-command-on-region (start end command
  843.                       &optional output-buffer replace)
  844.   "Execute string COMMAND in inferior shell with region as input.
  845. Normally display output (if any) in temp buffer `*Shell Command Output*';
  846. Prefix arg means replace the region with it.
  847.  
  848. The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, REPLACE.
  849. If REPLACE is non-nil, that means insert the output
  850. in place of text from START to END, putting point and mark around it.
  851.  
  852. If the output is one line, it is displayed in the echo area,
  853. but it is nonetheless available in buffer `*Shell Command Output*'
  854. even though that buffer is not automatically displayed.
  855. If there is no output, or if output is inserted in the current buffer,
  856. then `*Shell Command Output*' is deleted.
  857.  
  858. If the optional fourth argument OUTPUT-BUFFER is non-nil,
  859. that says to put the output in some other buffer.
  860. If OUTPUT-BUFFER is a buffer or buffer name, put the output there.
  861. If OUTPUT-BUFFER is not a buffer and not nil,
  862. insert output in the current buffer.
  863. In either case, the output is inserted after point (leaving mark after it)."
  864.   (interactive (let ((string
  865.               ;; Do this before calling region-beginning
  866.               ;; and region-end, in case subprocess output
  867.               ;; relocates them while we are in the minibuffer.
  868.               (read-from-minibuffer "Shell command on region: "
  869.                         nil nil nil
  870.                         'shell-command-history)))
  871.          ;; call-interactively recognizes region-beginning and
  872.          ;; region-end specially, leaving them in the history.
  873.          (list (region-beginning) (region-end)
  874.                string
  875.                current-prefix-arg
  876.                current-prefix-arg)))
  877.   (if (or replace
  878.       (and output-buffer
  879.            (not (or (bufferp output-buffer) (stringp output-buffer))))
  880.       (equal (buffer-name (current-buffer)) "*Shell Command Output*"))
  881.       ;; Replace specified region with output from command.
  882.       (let ((swap (and replace (< start end))))
  883.     ;; Don't muck with mark unless REPLACE says we should.
  884.     (goto-char start)
  885.     (and replace (push-mark))
  886.     (call-process-region start end shell-file-name t t nil
  887.                  shell-command-switch command)
  888.     (let ((shell-buffer (get-buffer "*Shell Command Output*")))
  889.       (and shell-buffer (not (eq shell-buffer (current-buffer)))
  890.            (kill-buffer shell-buffer)))
  891.     ;; Don't muck with mark unless REPLACE says we should.
  892.     (and replace swap (exchange-point-and-mark)))
  893.     ;; No prefix argument: put the output in a temp buffer,
  894.     ;; replacing its entire contents.
  895.     (let ((buffer (get-buffer-create
  896.            (or output-buffer "*Shell Command Output*")))
  897.       (success nil))
  898.       (unwind-protect
  899.       (if (eq buffer (current-buffer))
  900.           ;; If the input is the same buffer as the output,
  901.           ;; delete everything but the specified region,
  902.           ;; then replace that region with the output.
  903.           (progn (setq buffer-read-only nil)
  904.              (delete-region (max start end) (point-max))
  905.              (delete-region (point-min) (min start end))
  906.              (call-process-region (point-min) (point-max)
  907.                       shell-file-name t t nil
  908.                       shell-command-switch command)
  909.              (setq success t))
  910.         ;; Clear the output buffer, then run the command with output there.
  911.         (save-excursion
  912.           (set-buffer buffer)
  913.           (setq buffer-read-only nil)
  914.           (erase-buffer))
  915.         (call-process-region start end shell-file-name
  916.                  nil buffer nil
  917.                  shell-command-switch command)
  918.         (setq success t))
  919.     ;; Report the amount of output.
  920.     (let ((lines (save-excursion
  921.                (set-buffer buffer)
  922.                (if (= (buffer-size) 0)
  923.                0
  924.              (count-lines (point-min) (point-max))))))
  925.       (cond ((= lines 0)
  926.          (if success
  927.              (message "(Shell command completed with no output)"))
  928.          (kill-buffer buffer))
  929.         ((and success (= lines 1))
  930.          (message "%s"
  931.               (save-excursion
  932.                 (set-buffer buffer)
  933.                 (goto-char (point-min))
  934.                 (buffer-substring (point)
  935.                           (progn (end-of-line) (point))))))
  936.         (t 
  937.          (save-excursion
  938.            (set-buffer buffer)
  939.            (goto-char (point-min)))
  940.          (display-buffer buffer))))))))
  941.  
  942. (defconst universal-argument-map
  943.   (let ((map (make-sparse-keymap)))
  944.     (define-key map [t] 'universal-argument-other-key)
  945.     (define-key map (vector meta-prefix-char t) 'universal-argument-other-key)
  946.     (define-key map [switch-frame] nil)
  947.     (define-key map [?\C-u] 'universal-argument-more)
  948.     (define-key map [?-] 'universal-argument-minus)
  949.     (define-key map [?0] 'digit-argument)
  950.     (define-key map [?1] 'digit-argument)
  951.     (define-key map [?2] 'digit-argument)
  952.     (define-key map [?3] 'digit-argument)
  953.     (define-key map [?4] 'digit-argument)
  954.     (define-key map [?5] 'digit-argument)
  955.     (define-key map [?6] 'digit-argument)
  956.     (define-key map [?7] 'digit-argument)
  957.     (define-key map [?8] 'digit-argument)
  958.     (define-key map [?9] 'digit-argument)
  959.     map)
  960.   "Keymap used while processing \\[universal-argument].")
  961.  
  962. (defvar universal-argument-num-events nil
  963.   "Number of argument-specifying events read by `universal-argument'.
  964. `universal-argument-other-key' uses this to discard those events
  965. from (this-command-keys), and reread only the final command.")
  966.  
  967. (defun universal-argument ()
  968.   "Begin a numeric argument for the following command.
  969. Digits or minus sign following \\[universal-argument] make up the numeric argument.
  970. \\[universal-argument] following the digits or minus sign ends the argument.
  971. \\[universal-argument] without digits or minus sign provides 4 as argument.
  972. Repeating \\[universal-argument] without digits or minus sign
  973.  multiplies the argument by 4 each time."
  974.   (interactive)
  975.   (setq prefix-arg (list 4))
  976.   (setq universal-argument-num-events (length (this-command-keys)))
  977.   (setq overriding-terminal-local-map universal-argument-map))
  978.  
  979. ;; A subsequent C-u means to multiply the factor by 4 if we've typed
  980. ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
  981. (defun universal-argument-more (arg)
  982.   (interactive "P")
  983.   (if (consp arg)
  984.       (setq prefix-arg (list (* 4 (car arg))))
  985.     (setq prefix-arg arg)
  986.     (setq overriding-terminal-local-map nil))
  987.   (setq universal-argument-num-events (length (this-command-keys))))
  988.  
  989. (defun negative-argument (arg)
  990.   "Begin a negative numeric argument for the next command.
  991. \\[universal-argument] following digits or minus sign ends the argument."
  992.   (interactive "P")
  993.   (cond ((integerp arg)
  994.      (setq prefix-arg (- arg)))
  995.     ((eq arg '-)
  996.      (setq prefix-arg nil))
  997.     (t
  998.      (setq prefix-arg '-)))
  999.   (setq universal-argument-num-events (length (this-command-keys)))
  1000.   (setq overriding-terminal-local-map universal-argument-map))
  1001.  
  1002. (defun digit-argument (arg)
  1003.   "Part of the numeric argument for the next command.
  1004. \\[universal-argument] following digits or minus sign ends the argument."
  1005.   (interactive "P")
  1006.   (let ((digit (- (logand last-command-char ?\177) ?0)))
  1007.     (cond ((integerp arg)
  1008.        (setq prefix-arg (+ (* arg 10)
  1009.                    (if (< arg 0) (- digit) digit))))
  1010.       ((eq arg '-)
  1011.        ;; Treat -0 as just -, so that -01 will work.
  1012.        (setq prefix-arg (if (zerop digit) '- (- digit))))
  1013.       (t
  1014.        (setq prefix-arg digit))))
  1015.   (setq universal-argument-num-events (length (this-command-keys)))
  1016.   (setq overriding-terminal-local-map universal-argument-map))
  1017.  
  1018. ;; For backward compatibility, minus with no modifiers is an ordinary
  1019. ;; command if digits have already been entered.
  1020. (defun universal-argument-minus (arg)
  1021.   (interactive "P")
  1022.   (if (integerp arg)
  1023.       (universal-argument-other-key arg)
  1024.     (negative-argument arg)))
  1025.  
  1026. ;; Anything else terminates the argument and is left in the queue to be
  1027. ;; executed as a command.
  1028. (defun universal-argument-other-key (arg)
  1029.   (interactive "P")
  1030.   (setq prefix-arg arg)
  1031.   (let* ((key (this-command-keys))
  1032.      (keylist (listify-key-sequence key)))
  1033.     (setq unread-command-events
  1034.       (append (nthcdr universal-argument-num-events keylist)
  1035.           unread-command-events)))
  1036.   (reset-this-command-lengths)
  1037.   (setq overriding-terminal-local-map nil))
  1038.  
  1039. (defun forward-to-indentation (arg)
  1040.   "Move forward ARG lines and position at first nonblank character."
  1041.   (interactive "p")
  1042.   (forward-line arg)
  1043.   (skip-chars-forward " \t"))
  1044.  
  1045. (defun backward-to-indentation (arg)
  1046.   "Move backward ARG lines and position at first nonblank character."
  1047.   (interactive "p")
  1048.   (forward-line (- arg))
  1049.   (skip-chars-forward " \t"))
  1050.  
  1051. (defvar kill-whole-line nil
  1052.   "*If non-nil, `kill-line' with no arg at beg of line kills the whole line.")
  1053.  
  1054. (defun kill-line (&optional arg)
  1055.   "Kill the rest of the current line; if no nonblanks there, kill thru newline.
  1056. With prefix argument, kill that many lines from point.
  1057. Negative arguments kill lines backward.
  1058.  
  1059. When calling from a program, nil means \"no arg\",
  1060. a number counts as a prefix arg.
  1061.  
  1062. If `kill-whole-line' is non-nil, then kill the whole line
  1063. when given no argument at the beginning of a line."
  1064.   (interactive "P")
  1065.   (kill-region (point)
  1066.            ;; It is better to move point to the other end of the kill
  1067.            ;; before killing.  That way, in a read-only buffer, point
  1068.            ;; moves across the text that is copied to the kill ring.
  1069.            ;; The choice has no effect on undo now that undo records
  1070.            ;; the value of point from before the command was run.
  1071.            (progn
  1072.          (if arg
  1073.              (forward-line (prefix-numeric-value arg))
  1074.            (if (eobp)
  1075.                (signal 'end-of-buffer nil))
  1076.            (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
  1077.                (forward-line 1)
  1078.              (end-of-line)))
  1079.          (point))))
  1080.  
  1081. ;;;; Window system cut and paste hooks.
  1082.  
  1083. (defvar interprogram-cut-function nil
  1084.   "Function to call to make a killed region available to other programs.
  1085.  
  1086. Most window systems provide some sort of facility for cutting and
  1087. pasting text between the windows of different programs.
  1088. This variable holds a function that Emacs calls whenever text
  1089. is put in the kill ring, to make the new kill available to other
  1090. programs.
  1091.  
  1092. The function takes one or two arguments.
  1093. The first argument, TEXT, is a string containing
  1094. the text which should be made available.
  1095. The second, PUSH, if non-nil means this is a \"new\" kill;
  1096. nil means appending to an \"old\" kill.")
  1097.  
  1098. (defvar interprogram-paste-function nil
  1099.   "Function to call to get text cut from other programs.
  1100.  
  1101. Most window systems provide some sort of facility for cutting and
  1102. pasting text between the windows of different programs.
  1103. This variable holds a function that Emacs calls to obtain
  1104. text that other programs have provided for pasting.
  1105.  
  1106. The function should be called with no arguments.  If the function
  1107. returns nil, then no other program has provided such text, and the top
  1108. of the Emacs kill ring should be used.  If the function returns a
  1109. string, that string should be put in the kill ring as the latest kill.
  1110.  
  1111. Note that the function should return a string only if a program other
  1112. than Emacs has provided a string for pasting; if Emacs provided the
  1113. most recent string, the function should return nil.  If it is
  1114. difficult to tell whether Emacs or some other program provided the
  1115. current string, it is probably good enough to return nil if the string
  1116. is equal (according to `string=') to the last text Emacs provided.")
  1117.  
  1118.  
  1119.  
  1120. ;;;; The kill ring data structure.
  1121.  
  1122. (defvar kill-ring nil
  1123.   "List of killed text sequences.
  1124. Since the kill ring is supposed to interact nicely with cut-and-paste
  1125. facilities offered by window systems, use of this variable should
  1126. interact nicely with `interprogram-cut-function' and
  1127. `interprogram-paste-function'.  The functions `kill-new',
  1128. `kill-append', and `current-kill' are supposed to implement this
  1129. interaction; you may want to use them instead of manipulating the kill
  1130. ring directly.")
  1131.  
  1132. (defconst kill-ring-max 30
  1133.   "*Maximum length of kill ring before oldest elements are thrown away.")
  1134.  
  1135. (defvar kill-ring-yank-pointer nil
  1136.   "The tail of the kill ring whose car is the last thing yanked.")
  1137.  
  1138. (defun kill-new (string &optional replace)
  1139.   "Make STRING the latest kill in the kill ring.
  1140. Set the kill-ring-yank pointer to point to it.
  1141. If `interprogram-cut-function' is non-nil, apply it to STRING.
  1142. Optional second argument REPLACE non-nil means that STRING will replace
  1143. the front of the kill ring, rather than being added to the list."
  1144.   (and (fboundp 'menu-bar-update-yank-menu)
  1145.        (menu-bar-update-yank-menu string (and replace (car kill-ring))))
  1146.   (if replace
  1147.       (setcar kill-ring string)
  1148.     (setq kill-ring (cons string kill-ring))
  1149.     (if (> (length kill-ring) kill-ring-max)
  1150.     (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
  1151.   (setq kill-ring-yank-pointer kill-ring)
  1152.   (if interprogram-cut-function
  1153.       (funcall interprogram-cut-function string (not replace))))
  1154.  
  1155. (defun kill-append (string before-p)
  1156.   "Append STRING to the end of the latest kill in the kill ring.
  1157. If BEFORE-P is non-nil, prepend STRING to the kill.
  1158. If `interprogram-cut-function' is set, pass the resulting kill to
  1159. it."
  1160.   (kill-new (if before-p
  1161.         (concat string (car kill-ring))
  1162.           (concat (car kill-ring) string)) t))
  1163.  
  1164. (defun current-kill (n &optional do-not-move)
  1165.   "Rotate the yanking point by N places, and then return that kill.
  1166. If N is zero, `interprogram-paste-function' is set, and calling it
  1167. returns a string, then that string is added to the front of the
  1168. kill ring and returned as the latest kill.
  1169. If optional arg DO-NOT-MOVE is non-nil, then don't actually move the 
  1170. yanking point; just return the Nth kill forward."
  1171.   (let ((interprogram-paste (and (= n 0)
  1172.                  interprogram-paste-function
  1173.                  (funcall interprogram-paste-function))))
  1174.     (if interprogram-paste
  1175.     (progn
  1176.       ;; Disable the interprogram cut function when we add the new
  1177.       ;; text to the kill ring, so Emacs doesn't try to own the
  1178.       ;; selection, with identical text.
  1179.       (let ((interprogram-cut-function nil))
  1180.         (kill-new interprogram-paste))
  1181.       interprogram-paste)
  1182.       (or kill-ring (error "Kill ring is empty"))
  1183.       (let ((ARGth-kill-element
  1184.          (nthcdr (mod (- n (length kill-ring-yank-pointer))
  1185.               (length kill-ring))
  1186.              kill-ring)))
  1187.     (or do-not-move
  1188.         (setq kill-ring-yank-pointer ARGth-kill-element))
  1189.     (car ARGth-kill-element)))))
  1190.  
  1191.  
  1192.  
  1193. ;;;; Commands for manipulating the kill ring.
  1194.  
  1195. (defvar kill-read-only-ok nil
  1196.   "*Non-nil means don't signal an error for killing read-only text.")
  1197.  
  1198. (put 'text-read-only 'error-conditions
  1199.      '(text-read-only buffer-read-only error))
  1200. (put 'text-read-only 'error-message "Text is read-only")
  1201.  
  1202. (defun kill-region (beg end)
  1203.   "Kill between point and mark.
  1204. The text is deleted but saved in the kill ring.
  1205. The command \\[yank] can retrieve it from there.
  1206. \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
  1207. If the buffer is read-only, Emacs will beep and refrain from deleting
  1208. the text, but put the text in the kill ring anyway.  This means that
  1209. you can use the killing commands to copy text from a read-only buffer.
  1210.  
  1211. This is the primitive for programs to kill text (as opposed to deleting it).
  1212. Supply two arguments, character numbers indicating the stretch of text
  1213.  to be killed.
  1214. Any command that calls this function is a \"kill command\".
  1215. If the previous command was also a kill command,
  1216. the text killed this time appends to the text killed last time
  1217. to make one entry in the kill ring."
  1218.   (interactive "r")
  1219.   (cond
  1220.  
  1221.    ;; If the buffer is read-only, we should beep, in case the person
  1222.    ;; just isn't aware of this.  However, there's no harm in putting
  1223.    ;; the region's text in the kill ring, anyway.
  1224.    ((or (and buffer-read-only (not inhibit-read-only))
  1225.     (text-property-not-all beg end 'read-only nil))
  1226.     (copy-region-as-kill beg end)
  1227.     ;; This should always barf, and give us the correct error.
  1228.     (if kill-read-only-ok
  1229.     (message "Read only text copied to kill ring")
  1230.       (setq this-command 'kill-region)
  1231.       ;; Signal an error if the buffer is read-only.
  1232.       (barf-if-buffer-read-only)
  1233.       ;; If the buffer isn't read-only, the text is.
  1234.       (signal 'text-read-only (list (current-buffer)))))
  1235.  
  1236.    ;; In certain cases, we can arrange for the undo list and the kill
  1237.    ;; ring to share the same string object.  This code does that.
  1238.    ((not (or (eq buffer-undo-list t)
  1239.          (eq last-command 'kill-region)
  1240.          ;; Use = since positions may be numbers or markers.
  1241.          (= beg end)))
  1242.     ;; Don't let the undo list be truncated before we can even access it.
  1243.     (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100))
  1244.       (old-list buffer-undo-list)
  1245.       tail)
  1246.       (delete-region beg end)
  1247.       ;; Search back in buffer-undo-list for this string,
  1248.       ;; in case a change hook made property changes.
  1249.       (setq tail buffer-undo-list)
  1250.       (while (not (stringp (car (car tail))))
  1251.     (setq tail (cdr tail)))
  1252.       ;; Take the same string recorded for undo
  1253.       ;; and put it in the kill-ring.
  1254.       (kill-new (car (car tail)))))
  1255.  
  1256.    (t
  1257.     (copy-region-as-kill beg end)
  1258.     (delete-region beg end)))
  1259.   (setq this-command 'kill-region))
  1260.  
  1261. ;; copy-region-as-kill no longer sets this-command, because it's confusing
  1262. ;; to get two copies of the text when the user accidentally types M-w and
  1263. ;; then corrects it with the intended C-w.
  1264. (defun copy-region-as-kill (beg end)
  1265.   "Save the region as if killed, but don't kill it.
  1266. If `interprogram-cut-function' is non-nil, also save the text for a window
  1267. system cut and paste."
  1268.   (interactive "r")
  1269.   (if (eq last-command 'kill-region)
  1270.       (kill-append (buffer-substring beg end) (< end beg))
  1271.     (kill-new (buffer-substring beg end)))
  1272.   nil)
  1273.  
  1274. (defun kill-ring-save (beg end)
  1275.   "Save the region as if killed, but don't kill it.
  1276. This command is similar to `copy-region-as-kill', except that it gives
  1277. visual feedback indicating the extent of the region being copied.
  1278. If `interprogram-cut-function' is non-nil, also save the text for a window
  1279. system cut and paste."
  1280.   (interactive "r")
  1281.   (copy-region-as-kill beg end)
  1282.   (if (interactive-p)
  1283.       (let ((other-end (if (= (point) beg) end beg))
  1284.         (opoint (point))
  1285.         ;; Inhibit quitting so we can make a quit here
  1286.         ;; look like a C-g typed as a command.
  1287.         (inhibit-quit t))
  1288.     (if (pos-visible-in-window-p other-end (selected-window))
  1289.         (progn
  1290.           ;; Swap point and mark.
  1291.           (set-marker (mark-marker) (point) (current-buffer))
  1292.           (goto-char other-end)
  1293.           (sit-for 1)
  1294.           ;; Swap back.
  1295.           (set-marker (mark-marker) other-end (current-buffer))
  1296.           (goto-char opoint)
  1297.           ;; If user quit, deactivate the mark
  1298.           ;; as C-g would as a command.
  1299.           (and quit-flag mark-active
  1300.            (deactivate-mark)))
  1301.       (let* ((killed-text (current-kill 0))
  1302.          (message-len (min (length killed-text) 40)))
  1303.         (if (= (point) beg)
  1304.         ;; Don't say "killed"; that is misleading.
  1305.         (message "Saved text until \"%s\""
  1306.             (substring killed-text (- message-len)))
  1307.           (message "Saved text from \"%s\""
  1308.               (substring killed-text 0 message-len))))))))
  1309.  
  1310. (defun append-next-kill ()
  1311.   "Cause following command, if it kills, to append to previous kill."
  1312.   (interactive)
  1313.   (if (interactive-p)
  1314.       (progn
  1315.     (setq this-command 'kill-region)
  1316.     (message "If the next command is a kill, it will append"))
  1317.     (setq last-command 'kill-region)))
  1318.  
  1319. (defun yank-pop (arg)
  1320.   "Replace just-yanked stretch of killed text with a different stretch.
  1321. This command is allowed only immediately after a `yank' or a `yank-pop'.
  1322. At such a time, the region contains a stretch of reinserted
  1323. previously-killed text.  `yank-pop' deletes that text and inserts in its
  1324. place a different stretch of killed text.
  1325.  
  1326. With no argument, the previous kill is inserted.
  1327. With argument N, insert the Nth previous kill.
  1328. If N is negative, this is a more recent kill.
  1329.  
  1330. The sequence of kills wraps around, so that after the oldest one
  1331. comes the newest one."
  1332.   (interactive "*p")
  1333.   (if (not (eq last-command 'yank))
  1334.       (error "Previous command was not a yank"))
  1335.   (setq this-command 'yank)
  1336.   (let ((inhibit-read-only t)
  1337.     (before (< (point) (mark t))))
  1338.     (delete-region (point) (mark t))
  1339.     (set-marker (mark-marker) (point) (current-buffer))
  1340.     (insert (current-kill arg))
  1341.     (if before
  1342.     ;; This is like exchange-point-and-mark, but doesn't activate the mark.
  1343.     ;; It is cleaner to avoid activation, even though the command
  1344.     ;; loop would deactivate the mark because we inserted text.
  1345.     (goto-char (prog1 (mark t)
  1346.              (set-marker (mark-marker) (point) (current-buffer))))))
  1347.   nil)
  1348.  
  1349. (defun yank (&optional arg)
  1350.   "Reinsert the last stretch of killed text.
  1351. More precisely, reinsert the stretch of killed text most recently
  1352. killed OR yanked.  Put point at end, and set mark at beginning.
  1353. With just C-u as argument, same but put point at beginning (and mark at end).
  1354. With argument N, reinsert the Nth most recently killed stretch of killed
  1355. text.
  1356. See also the command \\[yank-pop]."
  1357.   (interactive "*P")
  1358.   ;; If we don't get all the way thru, make last-command indicate that
  1359.   ;; for the following command.
  1360.   (setq this-command t)
  1361.   (push-mark (point))
  1362.   (insert (current-kill (cond
  1363.              ((listp arg) 0)
  1364.              ((eq arg '-) -1)
  1365.              (t (1- arg)))))
  1366.   (if (consp arg)
  1367.       ;; This is like exchange-point-and-mark, but doesn't activate the mark.
  1368.       ;; It is cleaner to avoid activation, even though the command
  1369.       ;; loop would deactivate the mark because we inserted text.
  1370.       (goto-char (prog1 (mark t)
  1371.            (set-marker (mark-marker) (point) (current-buffer)))))
  1372.   ;; If we do get all the way thru, make this-command indicate that.
  1373.   (setq this-command 'yank)
  1374.   nil)
  1375.  
  1376. (defun rotate-yank-pointer (arg)
  1377.   "Rotate the yanking point in the kill ring.
  1378. With argument, rotate that many kills forward (or backward, if negative)."
  1379.   (interactive "p")
  1380.   (current-kill arg))
  1381.  
  1382.  
  1383. (defun insert-buffer (buffer)
  1384.   "Insert after point the contents of BUFFER.
  1385. Puts mark after the inserted text.
  1386. BUFFER may be a buffer or a buffer name."
  1387.   (interactive
  1388.    (list
  1389.     (progn
  1390.       (barf-if-buffer-read-only)
  1391.       (read-buffer "Insert buffer: "
  1392.            (if (eq (selected-window) (next-window (selected-window)))
  1393.                (other-buffer (current-buffer))
  1394.              (window-buffer (next-window (selected-window))))
  1395.            t))))
  1396.   (or (bufferp buffer)
  1397.       (setq buffer (get-buffer buffer)))
  1398.   (let (start end newmark)
  1399.     (save-excursion
  1400.       (save-excursion
  1401.     (set-buffer buffer)
  1402.     (setq start (point-min) end (point-max)))
  1403.       (insert-buffer-substring buffer start end)
  1404.       (setq newmark (point)))
  1405.     (push-mark newmark))
  1406.   nil)
  1407.  
  1408. (defun append-to-buffer (buffer start end)
  1409.   "Append to specified buffer the text of the region.
  1410. It is inserted into that buffer before its point.
  1411.  
  1412. When calling from a program, give three arguments:
  1413. BUFFER (or buffer name), START and END.
  1414. START and END specify the portion of the current buffer to be copied."
  1415.   (interactive
  1416.    (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
  1417.      (region-beginning) (region-end)))
  1418.   (let ((oldbuf (current-buffer)))
  1419.     (save-excursion
  1420.       (set-buffer (get-buffer-create buffer))
  1421.       (insert-buffer-substring oldbuf start end))))
  1422.  
  1423. (defun prepend-to-buffer (buffer start end)
  1424.   "Prepend to specified buffer the text of the region.
  1425. It is inserted into that buffer after its point.
  1426.  
  1427. When calling from a program, give three arguments:
  1428. BUFFER (or buffer name), START and END.
  1429. START and END specify the portion of the current buffer to be copied."
  1430.   (interactive "BPrepend to buffer: \nr")
  1431.   (let ((oldbuf (current-buffer)))
  1432.     (save-excursion
  1433.       (set-buffer (get-buffer-create buffer))
  1434.       (save-excursion
  1435.     (insert-buffer-substring oldbuf start end)))))
  1436.  
  1437. (defun copy-to-buffer (buffer start end)
  1438.   "Copy to specified buffer the text of the region.
  1439. It is inserted into that buffer, replacing existing text there.
  1440.  
  1441. When calling from a program, give three arguments:
  1442. BUFFER (or buffer name), START and END.
  1443. START and END specify the portion of the current buffer to be copied."
  1444.   (interactive "BCopy to buffer: \nr")
  1445.   (let ((oldbuf (current-buffer)))
  1446.     (save-excursion
  1447.       (set-buffer (get-buffer-create buffer))
  1448.       (erase-buffer)
  1449.       (save-excursion
  1450.     (insert-buffer-substring oldbuf start end)))))
  1451.  
  1452. (put 'mark-inactive 'error-conditions '(mark-inactive error))
  1453. (put 'mark-inactive 'error-message "The mark is not active now")
  1454.  
  1455. (defun mark (&optional force)
  1456.   "Return this buffer's mark value as integer; error if mark inactive.
  1457. If optional argument FORCE is non-nil, access the mark value
  1458. even if the mark is not currently active, and return nil
  1459. if there is no mark at all.
  1460.  
  1461. If you are using this in an editing command, you are most likely making
  1462. a mistake; see the documentation of `set-mark'."
  1463.   (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
  1464.       (marker-position (mark-marker))
  1465.     (signal 'mark-inactive nil)))
  1466.  
  1467. ;; Many places set mark-active directly, and several of them failed to also
  1468. ;; run deactivate-mark-hook.  This shorthand should simplify.
  1469. (defsubst deactivate-mark ()
  1470.   "Deactivate the mark by setting `mark-active' to nil.
  1471. \(That makes a difference only in Transient Mark mode.)
  1472. Also runs the hook `deactivate-mark-hook'."
  1473.   (if transient-mark-mode
  1474.       (progn
  1475.     (setq mark-active nil)
  1476.     (run-hooks 'deactivate-mark-hook))))
  1477.  
  1478. (defun set-mark (pos)
  1479.   "Set this buffer's mark to POS.  Don't use this function!
  1480. That is to say, don't use this function unless you want
  1481. the user to see that the mark has moved, and you want the previous
  1482. mark position to be lost.
  1483.  
  1484. Normally, when a new mark is set, the old one should go on the stack.
  1485. This is why most applications should use push-mark, not set-mark.
  1486.  
  1487. Novice Emacs Lisp programmers often try to use the mark for the wrong
  1488. purposes.  The mark saves a location for the user's convenience.
  1489. Most editing commands should not alter the mark.
  1490. To remember a location for internal use in the Lisp program,
  1491. store it in a Lisp variable.  Example:
  1492.  
  1493.    (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
  1494.  
  1495.   (if pos
  1496.       (progn
  1497.     (setq mark-active t)
  1498.     (run-hooks 'activate-mark-hook)
  1499.     (set-marker (mark-marker) pos (current-buffer)))
  1500.     ;; Normally we never clear mark-active except in Transient Mark mode.
  1501.     ;; But when we actually clear out the mark value too,
  1502.     ;; we must clear mark-active in any mode.
  1503.     (setq mark-active nil)
  1504.     (run-hooks 'deactivate-mark-hook)
  1505.     (set-marker (mark-marker) nil)))
  1506.  
  1507. (defvar mark-ring nil
  1508.   "The list of former marks of the current buffer, most recent first.")
  1509. (make-variable-buffer-local 'mark-ring)
  1510. (put 'mark-ring 'permanent-local t)
  1511.  
  1512. (defconst mark-ring-max 16
  1513.   "*Maximum size of mark ring.  Start discarding off end if gets this big.")
  1514.  
  1515. (defvar global-mark-ring nil
  1516.   "The list of saved global marks, most recent first.")
  1517.  
  1518. (defconst global-mark-ring-max 16
  1519.   "*Maximum size of global mark ring.  \
  1520. Start discarding off end if gets this big.")
  1521.  
  1522. (defun set-mark-command (arg)
  1523.   "Set mark at where point is, or jump to mark.
  1524. With no prefix argument, set mark, push old mark position on local mark
  1525. ring, and push mark on global mark ring.
  1526. With argument, jump to mark, and pop a new position for mark off the ring
  1527. \(does not affect global mark ring\).
  1528.  
  1529. Novice Emacs Lisp programmers often try to use the mark for the wrong
  1530. purposes.  See the documentation of `set-mark' for more information."
  1531.   (interactive "P")
  1532.   (if (null arg)
  1533.       (progn
  1534.     (push-mark nil nil t))
  1535.     (if (null (mark t))
  1536.     (error "No mark set in this buffer")
  1537.       (goto-char (mark t))
  1538.       (pop-mark))))
  1539.  
  1540. (defun push-mark (&optional location nomsg activate)
  1541.   "Set mark at LOCATION (point, by default) and push old mark on mark ring.
  1542. If the last global mark pushed was not in the current buffer,
  1543. also push LOCATION on the global mark ring.
  1544. Display `Mark set' unless the optional second arg NOMSG is non-nil.
  1545. In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil.
  1546.  
  1547. Novice Emacs Lisp programmers often try to use the mark for the wrong
  1548. purposes.  See the documentation of `set-mark' for more information.
  1549.  
  1550. In Transient Mark mode, this does not activate the mark."
  1551.   (if (null (mark t))
  1552.       nil
  1553.     (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
  1554.     (if (> (length mark-ring) mark-ring-max)
  1555.     (progn
  1556.       (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
  1557.       (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
  1558.   (set-marker (mark-marker) (or location (point)) (current-buffer))
  1559.   ;; Now push the mark on the global mark ring.
  1560.   (if (and global-mark-ring
  1561.        (eq (marker-buffer (car global-mark-ring)) (current-buffer)))
  1562.       ;; The last global mark pushed was in this same buffer.
  1563.       ;; Don't push another one.
  1564.       nil
  1565.     (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring))
  1566.     (if (> (length global-mark-ring) global-mark-ring-max)
  1567.     (progn
  1568.       (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
  1569.                nil)
  1570.       (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil))))
  1571.   (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
  1572.       (message "Mark set"))
  1573.   (if (or activate (not transient-mark-mode))
  1574.       (set-mark (mark t)))
  1575.   nil)
  1576.  
  1577. (defun pop-mark ()
  1578.   "Pop off mark ring into the buffer's actual mark.
  1579. Does not set point.  Does nothing if mark ring is empty."
  1580.   (if mark-ring
  1581.       (progn
  1582.     (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
  1583.     (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer))
  1584.     (deactivate-mark)
  1585.     (move-marker (car mark-ring) nil)
  1586.     (if (null (mark t)) (ding))
  1587.     (setq mark-ring (cdr mark-ring)))))
  1588.  
  1589. (define-function 'exchange-dot-and-mark 'exchange-point-and-mark)
  1590. (defun exchange-point-and-mark ()
  1591.   "Put the mark where point is now, and point where the mark is now.
  1592. This command works even when the mark is not active,
  1593. and it reactivates the mark."
  1594.   (interactive nil)
  1595.   (let ((omark (mark t)))
  1596.     (if (null omark)
  1597.     (error "No mark set in this buffer"))
  1598.     (set-mark (point))
  1599.     (goto-char omark)
  1600.     nil))
  1601.  
  1602. (defun transient-mark-mode (arg)
  1603.   "Toggle Transient Mark mode.
  1604. With arg, turn Transient Mark mode on if arg is positive, off otherwise.
  1605.  
  1606. In Transient Mark mode, when the mark is active, the region is highlighted.
  1607. Changing the buffer \"deactivates\" the mark.
  1608. So do certain other operations that set the mark
  1609. but whose main purpose is something else--for example,
  1610. incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
  1611.   (interactive "P")
  1612.   (setq transient-mark-mode
  1613.     (if (null arg)
  1614.         (not transient-mark-mode)
  1615.       (> (prefix-numeric-value arg) 0))))
  1616.  
  1617. (defun pop-global-mark ()
  1618.   "Pop off global mark ring and jump to the top location."
  1619.   (interactive)
  1620.   ;; Pop entries which refer to non-existent buffers.
  1621.   (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
  1622.     (setq global-mark-ring (cdr global-mark-ring)))
  1623.   (or global-mark-ring
  1624.       (error "No global mark set"))
  1625.   (let* ((marker (car global-mark-ring))
  1626.      (buffer (marker-buffer marker))
  1627.      (position (marker-position marker)))
  1628.     (setq global-mark-ring (nconc (cdr global-mark-ring)
  1629.                   (list (car global-mark-ring))))
  1630.     (set-buffer buffer)
  1631.     (or (and (>= position (point-min))
  1632.          (<= position (point-max)))
  1633.     (widen))
  1634.     (goto-char position)
  1635.     (switch-to-buffer buffer)))
  1636.  
  1637. (defvar next-line-add-newlines t
  1638.   "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error.")
  1639.  
  1640. (defun next-line (arg)
  1641.   "Move cursor vertically down ARG lines.
  1642. If there is no character in the target line exactly under the current column,
  1643. the cursor is positioned after the character in that line which spans this
  1644. column, or at the end of the line if it is not long enough.
  1645. If there is no line in the buffer after this one, behavior depends on the
  1646. value of `next-line-add-newlines'.  If non-nil, it inserts a newline character
  1647. to create a line, and moves the cursor to that line.  Otherwise it moves the
  1648. cursor to the end of the buffer.
  1649.  
  1650. The command \\[set-goal-column] can be used to create
  1651. a semipermanent goal column to which this command always moves.
  1652. Then it does not try to move vertically.  This goal column is stored
  1653. in `goal-column', which is nil when there is none.
  1654.  
  1655. If you are thinking of using this in a Lisp program, consider
  1656. using `forward-line' instead.  It is usually easier to use
  1657. and more reliable (no dependence on goal column, etc.)."
  1658.   (interactive "p")
  1659.   (if (and next-line-add-newlines (= arg 1))
  1660.       (let ((opoint (point)))
  1661.     (end-of-line)
  1662.     (if (eobp)
  1663.         (newline 1)
  1664.       (goto-char opoint)
  1665.       (line-move arg)))
  1666.     (if (interactive-p)
  1667.     (condition-case nil
  1668.         (line-move arg)
  1669.       ((beginning-of-buffer end-of-buffer) (ding)))
  1670.       (line-move arg)))
  1671.   nil)
  1672.  
  1673. (defun previous-line (arg)
  1674.   "Move cursor vertically up ARG lines.
  1675. If there is no character in the target line exactly over the current column,
  1676. the cursor is positioned after the character in that line which spans this
  1677. column, or at the end of the line if it is not long enough.
  1678.  
  1679. The command \\[set-goal-column] can be used to create
  1680. a semipermanent goal column to which this command always moves.
  1681. Then it does not try to move vertically.
  1682.  
  1683. If you are thinking of using this in a Lisp program, consider using
  1684. `forward-line' with a negative argument instead.  It is usually easier
  1685. to use and more reliable (no dependence on goal column, etc.)."
  1686.   (interactive "p")
  1687.   (if (interactive-p)
  1688.       (condition-case nil
  1689.       (line-move (- arg))
  1690.     ((beginning-of-buffer end-of-buffer) (ding)))
  1691.     (line-move (- arg)))
  1692.   nil)
  1693.  
  1694. (defconst track-eol nil
  1695.   "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
  1696. This means moving to the end of each line moved onto.
  1697. The beginning of a blank line does not count as the end of a line.")
  1698.  
  1699. (defvar goal-column nil
  1700.   "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil.")
  1701. (make-variable-buffer-local 'goal-column)
  1702.  
  1703. (defvar temporary-goal-column 0
  1704.   "Current goal column for vertical motion.
  1705. It is the column where point was
  1706. at the start of current run of vertical motion commands.
  1707. When the `track-eol' feature is doing its job, the value is 9999.")
  1708.  
  1709. (defvar line-move-ignore-invisible nil
  1710.   "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
  1711. Outline mode sets this.")
  1712.  
  1713. ;; This is the guts of next-line and previous-line.
  1714. ;; Arg says how many lines to move.
  1715. (defun line-move (arg)
  1716.   ;; Don't run any point-motion hooks, and disregard intangibility,
  1717.   ;; for intermediate positions.
  1718.   (let ((inhibit-point-motion-hooks t)
  1719.     (opoint (point))
  1720.     new)
  1721.     (unwind-protect
  1722.     (progn
  1723.       (if (not (or (eq last-command 'next-line)
  1724.                (eq last-command 'previous-line)))
  1725.           (setq temporary-goal-column
  1726.             (if (and track-eol (eolp)
  1727.                  ;; Don't count beg of empty line as end of line
  1728.                  ;; unless we just did explicit end-of-line.
  1729.                  (or (not (bolp)) (eq last-command 'end-of-line)))
  1730.             9999
  1731.               (current-column))))
  1732.       (if (and (not (integerp selective-display))
  1733.            (not line-move-ignore-invisible))
  1734.           ;; Use just newline characters.
  1735.           (or (if (> arg 0)
  1736.               (progn (if (> arg 1) (forward-line (1- arg)))
  1737.                  ;; This way of moving forward ARG lines
  1738.                  ;; verifies that we have a newline after the last one.
  1739.                  ;; It doesn't get confused by intangible text.
  1740.                  (end-of-line)
  1741.                  (zerop (forward-line 1)))
  1742.             (and (zerop (forward-line arg))
  1743.              (bolp)))
  1744.           (signal (if (< arg 0)
  1745.                   'beginning-of-buffer
  1746.                 'end-of-buffer)
  1747.               nil))
  1748.         ;; Move by arg lines, but ignore invisible ones.
  1749.         (while (> arg 0)
  1750.           (end-of-line)
  1751.           (and (zerop (vertical-motion 1))
  1752.            (signal 'end-of-buffer nil))
  1753.           ;; If the following character is currently invisible,
  1754.           ;; skip all characters with that same `invisible' property value.
  1755.           (while (and (not (eobp))
  1756.               (let ((prop
  1757.                  (get-char-property (point) 'invisible)))
  1758.                 (if (eq buffer-invisibility-spec t)
  1759.                 prop
  1760.                   (or (memq prop buffer-invisibility-spec)
  1761.                   (assq prop buffer-invisibility-spec)))))
  1762.         (if (get-text-property (point) 'invisible)
  1763.             (goto-char (next-single-property-change (point) 'invisible))
  1764.           (goto-char (next-overlay-change (point)))))
  1765.           (setq arg (1- arg)))
  1766.         (while (< arg 0)
  1767.           (beginning-of-line)
  1768.           (and (zerop (vertical-motion -1))
  1769.            (signal 'beginning-of-buffer nil))
  1770.           (while (and (not (bobp))
  1771.               (let ((prop
  1772.                  (get-char-property (1- (point)) 'invisible)))
  1773.                 (if (eq buffer-invisibility-spec t)
  1774.                 prop
  1775.                   (or (memq prop buffer-invisibility-spec)
  1776.                   (assq prop buffer-invisibility-spec)))))
  1777.         (if (get-text-property (1- (point)) 'invisible)
  1778.             (goto-char (previous-single-property-change (point) 'invisible))
  1779.           (goto-char (previous-overlay-change (point)))))
  1780.           (setq arg (1+ arg))))
  1781.       (move-to-column (or goal-column temporary-goal-column)))
  1782.       ;; Remember where we moved to, go back home,
  1783.       ;; then do the motion over again
  1784.       ;; in just one step, with intangibility and point-motion hooks
  1785.       ;; enabled this time.
  1786.       (setq new (point))
  1787.       (goto-char opoint)
  1788.       (setq inhibit-point-motion-hooks nil)
  1789.       (goto-char new)))
  1790.   nil)
  1791.  
  1792. ;;; Many people have said they rarely use this feature, and often type
  1793. ;;; it by accident.  Maybe it shouldn't even be on a key.
  1794. (put 'set-goal-column 'disabled t)
  1795.  
  1796. (defun set-goal-column (arg)
  1797.   "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
  1798. Those commands will move to this position in the line moved to
  1799. rather than trying to keep the same horizontal position.
  1800. With a non-nil argument, clears out the goal column
  1801. so that \\[next-line] and \\[previous-line] resume vertical motion.
  1802. The goal column is stored in the variable `goal-column'."
  1803.   (interactive "P")
  1804.   (if arg
  1805.       (progn
  1806.         (setq goal-column nil)
  1807.         (message "No goal column"))
  1808.     (setq goal-column (current-column))
  1809.     (message (substitute-command-keys
  1810.           "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
  1811.          goal-column))
  1812.   nil)
  1813.  
  1814. ;;; Partial support for horizontal autoscrolling.  Someday, this feature
  1815. ;;; will be built into the C level and all the (hscroll-point-visible) calls
  1816. ;;; will go away.
  1817.  
  1818. (defvar hscroll-step 0
  1819.    "*The number of columns to try scrolling a window by when point moves out.
  1820. If that fails to bring point back on frame, point is centered instead.
  1821. If this is zero, point is always centered after it moves off frame.")
  1822.  
  1823. (defun hscroll-point-visible ()
  1824.   "Scrolls the selected window horizontally to make point visible."
  1825.   (save-excursion
  1826.     (set-buffer (window-buffer))
  1827.     (if (not (or truncate-lines
  1828.          (> (window-hscroll) 0)
  1829.          (and truncate-partial-width-windows
  1830.               (< (window-width) (frame-width)))))
  1831.     ;; Point is always visible when lines are wrapped.
  1832.     ()
  1833.       ;; If point is on the invisible part of the line before window-start,
  1834.       ;; then hscrolling can't bring it back, so reset window-start first.
  1835.       (and (< (point) (window-start))
  1836.        (let ((ws-bol (save-excursion
  1837.                (goto-char (window-start))
  1838.                (beginning-of-line)
  1839.                (point))))
  1840.          (and (>= (point) ws-bol)
  1841.           (set-window-start nil ws-bol))))
  1842.       (let* ((here (hscroll-window-column))
  1843.          (left (min (window-hscroll) 1))
  1844.          (right (1- (window-width))))
  1845.     ;; Allow for the truncation glyph, if we're not exactly at eol.
  1846.     (if (not (and (= here right)
  1847.               (= (following-char) ?\n)))
  1848.         (setq right (1- right)))
  1849.     (cond
  1850.      ;; If too far away, just recenter.  But don't show too much
  1851.      ;; white space off the end of the line.
  1852.      ((or (< here (- left  hscroll-step))
  1853.           (> here (+ right hscroll-step)))
  1854.       (let ((eol (save-excursion (end-of-line) (hscroll-window-column))))
  1855.         (scroll-left (min (- here (/ (window-width) 2))
  1856.                   (- eol (window-width) -5)))))
  1857.      ;; Within range.  Scroll by one step (or maybe not at all).
  1858.      ((< here left)
  1859.       (scroll-right hscroll-step))
  1860.      ((> here right)
  1861.       (scroll-left hscroll-step)))))))
  1862.  
  1863. ;; This function returns the window's idea of the display column of point,
  1864. ;; assuming that the window is already known to be truncated rather than
  1865. ;; wrapped, and that we've already handled the case where point is on the
  1866. ;; part of the line before window-start.  We ignore window-width; if point
  1867. ;; is beyond the right margin, we want to know how far.  The return value
  1868. ;; includes the effects of window-hscroll, window-start, and the prompt
  1869. ;; string in the minibuffer.  It may be negative due to hscroll.
  1870. (defun hscroll-window-column ()
  1871.   (let* ((hscroll (window-hscroll))
  1872.      (startpos (save-excursion
  1873.              (beginning-of-line)
  1874.              (if (= (point) (save-excursion
  1875.                       (goto-char (window-start))
  1876.                       (beginning-of-line)
  1877.                       (point)))
  1878.              (goto-char (window-start)))
  1879.              (point)))
  1880.      (hpos (+ (if (and (eq (selected-window) (minibuffer-window))
  1881.                (= 1 (window-start))
  1882.                (= startpos (point-min)))
  1883.               (minibuffer-prompt-width)
  1884.             0)
  1885.           (min 0 (- 1 hscroll))))
  1886.      val)
  1887.     (car (cdr (compute-motion startpos (cons hpos 0)
  1888.                   (point) (cons 0 1)
  1889.                   1000000 (cons hscroll 0) nil)))))
  1890.  
  1891.   
  1892. ;; rms: (1) The definitions of arrow keys should not simply restate
  1893. ;; what keys they are.  The arrow keys should run the ordinary commands.
  1894. ;; (2) The arrow keys are just one of many common ways of moving point
  1895. ;; within a line.  Real horizontal autoscrolling would be a good feature,
  1896. ;; but supporting it only for arrow keys is too incomplete to be desirable.
  1897.  
  1898. ;;;;; Make arrow keys do the right thing for improved terminal support
  1899. ;;;;; When we implement true horizontal autoscrolling, right-arrow and
  1900. ;;;;; left-arrow can lose the (if truncate-lines ...) clause and become
  1901. ;;;;; aliases.  These functions are bound to the corresponding keyboard
  1902. ;;;;; events in loaddefs.el.
  1903.  
  1904. ;;(defun right-arrow (arg)
  1905. ;;  "Move right one character on the screen (with prefix ARG, that many chars).
  1906. ;;Scroll right if needed to keep point horizontally onscreen."
  1907. ;;  (interactive "P")
  1908. ;;  (forward-char arg)
  1909. ;;  (hscroll-point-visible))
  1910.  
  1911. ;;(defun left-arrow (arg)
  1912. ;;  "Move left one character on the screen (with prefix ARG, that many chars).
  1913. ;;Scroll left if needed to keep point horizontally onscreen."
  1914. ;;  (interactive "P")
  1915. ;;  (backward-char arg)
  1916. ;;  (hscroll-point-visible))
  1917.  
  1918. (defun scroll-other-window-down (lines)
  1919.   "Scroll the \"other window\" down.
  1920. For more details, see the documentation for `scroll-other-window'."
  1921.   (interactive "P")
  1922.   (scroll-other-window
  1923.    ;; Just invert the argument's meaning.
  1924.    ;; We can do that without knowing which window it will be.
  1925.    (if (eq lines '-) nil
  1926.      (if (null lines) '-
  1927.        (- (prefix-numeric-value lines))))))
  1928. (define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
  1929.  
  1930. (defun beginning-of-buffer-other-window (arg)
  1931.   "Move point to the beginning of the buffer in the other window.
  1932. Leave mark at previous position.
  1933. With arg N, put point N/10 of the way from the true beginning."
  1934.   (interactive "P")
  1935.   (let ((orig-window (selected-window))
  1936.     (window (other-window-for-scrolling)))
  1937.     ;; We use unwind-protect rather than save-window-excursion
  1938.     ;; because the latter would preserve the things we want to change.
  1939.     (unwind-protect
  1940.     (progn
  1941.       (select-window window)
  1942.       ;; Set point and mark in that window's buffer.
  1943.       (beginning-of-buffer arg)
  1944.       ;; Set point accordingly.
  1945.       (recenter '(t)))
  1946.       (select-window orig-window))))
  1947.  
  1948. (defun end-of-buffer-other-window (arg)
  1949.   "Move point to the end of the buffer in the other window.
  1950. Leave mark at previous position.
  1951. With arg N, put point N/10 of the way from the true end."
  1952.   (interactive "P")
  1953.   ;; See beginning-of-buffer-other-window for comments.
  1954.   (let ((orig-window (selected-window))
  1955.     (window (other-window-for-scrolling)))
  1956.     (unwind-protect
  1957.     (progn
  1958.       (select-window window)
  1959.       (end-of-buffer arg)
  1960.       (recenter '(t)))
  1961.       (select-window orig-window))))
  1962.  
  1963. (defun transpose-chars (arg)
  1964.   "Interchange characters around point, moving forward one character.
  1965. With prefix arg ARG, effect is to take character before point
  1966. and drag it forward past ARG other characters (backward if ARG negative).
  1967. If no argument and at end of line, the previous two chars are exchanged."
  1968.   (interactive "*P")
  1969.   (and (null arg) (eolp) (forward-char -1))
  1970.   (transpose-subr 'forward-char (prefix-numeric-value arg)))
  1971.  
  1972. (defun transpose-words (arg)
  1973.   "Interchange words around point, leaving point at end of them.
  1974. With prefix arg ARG, effect is to take word before or around point
  1975. and drag it forward past ARG other words (backward if ARG negative).
  1976. If ARG is zero, the words around or after point and around or after mark
  1977. are interchanged."
  1978.   (interactive "*p")
  1979.   (transpose-subr 'forward-word arg))
  1980.  
  1981. (defun transpose-sexps (arg)
  1982.   "Like \\[transpose-words] but applies to sexps.
  1983. Does not work on a sexp that point is in the middle of
  1984. if it is a list or string."
  1985.   (interactive "*p")
  1986.   (transpose-subr 'forward-sexp arg))
  1987.  
  1988. (defun transpose-lines (arg)
  1989.   "Exchange current line and previous line, leaving point after both.
  1990. With argument ARG, takes previous line and moves it past ARG lines.
  1991. With argument 0, interchanges line point is in with line mark is in."
  1992.   (interactive "*p")
  1993.   (transpose-subr (function
  1994.            (lambda (arg)
  1995.              (if (= arg 1)
  1996.              (progn
  1997.                ;; Move forward over a line,
  1998.                ;; but create a newline if none exists yet.
  1999.                (end-of-line)
  2000.                (if (eobp)
  2001.                    (newline)
  2002.                  (forward-char 1)))
  2003.                (forward-line arg))))
  2004.           arg))
  2005.  
  2006. (defun transpose-subr (mover arg)
  2007.   (let (start1 end1 start2 end2)
  2008.     (if (= arg 0)
  2009.     (progn
  2010.       (save-excursion
  2011.         (funcall mover 1)
  2012.         (setq end2 (point))
  2013.         (funcall mover -1)
  2014.         (setq start2 (point))
  2015.         (goto-char (mark))
  2016.         (funcall mover 1)
  2017.         (setq end1 (point))
  2018.         (funcall mover -1)
  2019.         (setq start1 (point))
  2020.         (transpose-subr-1))
  2021.       (exchange-point-and-mark)))
  2022.     (while (> arg 0)
  2023.       (funcall mover -1)
  2024.       (setq start1 (point))
  2025.       (funcall mover 1)
  2026.       (setq end1 (point))
  2027.       (funcall mover 1)
  2028.       (setq end2 (point))
  2029.       (funcall mover -1)
  2030.       (setq start2 (point))
  2031.       (transpose-subr-1)
  2032.       (goto-char end2)
  2033.       (setq arg (1- arg)))
  2034.     (while (< arg 0)
  2035.       (funcall mover -1)
  2036.       (setq start2 (point))
  2037.       (funcall mover -1)
  2038.       (setq start1 (point))
  2039.       (funcall mover 1)
  2040.       (setq end1 (point))
  2041.       (funcall mover 1)
  2042.       (setq end2 (point))
  2043.       (transpose-subr-1)
  2044.       (setq arg (1+ arg)))))
  2045.  
  2046. (defun transpose-subr-1 ()
  2047.   (if (> (min end1 end2) (max start1 start2))
  2048.       (error "Don't have two things to transpose"))
  2049.   (let ((word1 (buffer-substring start1 end1))
  2050.     (word2 (buffer-substring start2 end2)))
  2051.     (delete-region start2 end2)
  2052.     (goto-char start2)
  2053.     (insert word1)
  2054.     (goto-char (if (< start1 start2) start1
  2055.          (+ start1 (- (length word1) (length word2)))))
  2056.     (delete-char (length word1))
  2057.     (insert word2)))
  2058.  
  2059. (defconst comment-column 32
  2060.   "*Column to indent right-margin comments to.
  2061. Setting this variable automatically makes it local to the current buffer.
  2062. Each mode establishes a different default value for this variable; you
  2063. can set the value for a particular mode using that mode's hook.")
  2064. (make-variable-buffer-local 'comment-column)
  2065.  
  2066. (defconst comment-start nil
  2067.   "*String to insert to start a new comment, or nil if no comment syntax.")
  2068.  
  2069. (defconst comment-start-skip nil
  2070.   "*Regexp to match the start of a comment plus everything up to its body.
  2071. If there are any \\(...\\) pairs, the comment delimiter text is held to begin
  2072. at the place matched by the close of the first pair.")
  2073.  
  2074. (defconst comment-end ""
  2075.   "*String to insert to end a new comment.
  2076. Should be an empty string if comments are terminated by end-of-line.")
  2077.  
  2078. (defconst comment-indent-hook nil
  2079.   "Obsolete variable for function to compute desired indentation for a comment.
  2080. This function is called with no args with point at the beginning of
  2081. the comment's starting delimiter.")
  2082.  
  2083. (defconst comment-indent-function
  2084.   '(lambda () comment-column)
  2085.   "Function to compute desired indentation for a comment.
  2086. This function is called with no args with point at the beginning of
  2087. the comment's starting delimiter.")
  2088.  
  2089. (defconst block-comment-start nil
  2090.   "*String to insert to start a new comment on a line by itself.
  2091. If nil, use `comment-start' instead.
  2092. Note that the regular expression `comment-start-skip' should skip this string
  2093. as well as the `comment-start' string.")
  2094.  
  2095. (defconst block-comment-end nil
  2096.   "*String to insert to end a new comment on a line by itself.
  2097. Should be an empty string if comments are terminated by end-of-line.
  2098. If nil, use `comment-end' instead.")
  2099.  
  2100. (defun indent-for-comment ()
  2101.   "Indent this line's comment to comment column, or insert an empty comment."
  2102.   (interactive "*")
  2103.   (let* ((empty (save-excursion (beginning-of-line)
  2104.                 (looking-at "[ \t]*$")))
  2105.      (starter (or (and empty block-comment-start) comment-start))
  2106.      (ender (or (and empty block-comment-end) comment-end)))
  2107.     (if (null starter)
  2108.     (error "No comment syntax defined")
  2109.       (let* ((eolpos (save-excursion (end-of-line) (point)))
  2110.          cpos indent begpos)
  2111.     (beginning-of-line)
  2112.     (if (re-search-forward comment-start-skip eolpos 'move)
  2113.         (progn (setq cpos (point-marker))
  2114.            ;; Find the start of the comment delimiter.
  2115.            ;; If there were paren-pairs in comment-start-skip,
  2116.            ;; position at the end of the first pair.
  2117.            (if (match-end 1)
  2118.                (goto-char (match-end 1))
  2119.              ;; If comment-start-skip matched a string with
  2120.              ;; internal whitespace (not final whitespace) then
  2121.              ;; the delimiter start at the end of that
  2122.              ;; whitespace.  Otherwise, it starts at the
  2123.              ;; beginning of what was matched.
  2124.              (skip-syntax-backward " " (match-beginning 0))
  2125.              (skip-syntax-backward "^ " (match-beginning 0)))))
  2126.     (setq begpos (point))
  2127.     ;; Compute desired indent.
  2128.     (if (= (current-column)
  2129.            (setq indent (if comment-indent-hook
  2130.                 (funcall comment-indent-hook)
  2131.                   (funcall comment-indent-function))))
  2132.         (goto-char begpos)
  2133.       ;; If that's different from current, change it.
  2134.       (skip-chars-backward " \t")
  2135.       (delete-region (point) begpos)
  2136.       (indent-to indent))
  2137.     ;; An existing comment?
  2138.     (if cpos 
  2139.         (progn (goto-char cpos)
  2140.            (set-marker cpos nil))
  2141.       ;; No, insert one.
  2142.       (insert starter)
  2143.       (save-excursion
  2144.         (insert ender)))))))
  2145.  
  2146. (defun set-comment-column (arg)
  2147.   "Set the comment column based on point.
  2148. With no arg, set the comment column to the current column.
  2149. With just minus as arg, kill any comment on this line.
  2150. With any other arg, set comment column to indentation of the previous comment
  2151.  and then align or create a comment on this line at that column."
  2152.   (interactive "P")
  2153.   (if (eq arg '-)
  2154.       (kill-comment nil)
  2155.     (if arg
  2156.     (progn
  2157.       (save-excursion
  2158.         (beginning-of-line)
  2159.         (re-search-backward comment-start-skip)
  2160.         (beginning-of-line)
  2161.         (re-search-forward comment-start-skip)
  2162.         (goto-char (match-beginning 0))
  2163.         (setq comment-column (current-column))
  2164.         (message "Comment column set to %d" comment-column))
  2165.       (indent-for-comment))
  2166.       (setq comment-column (current-column))
  2167.       (message "Comment column set to %d" comment-column))))
  2168.  
  2169. (defun kill-comment (arg)
  2170.   "Kill the comment on this line, if any.
  2171. With argument, kill comments on that many lines starting with this one."
  2172.   ;; this function loses in a lot of situations.  it incorrectly recognises
  2173.   ;; comment delimiters sometimes (ergo, inside a string), doesn't work
  2174.   ;; with multi-line comments, can kill extra whitespace if comment wasn't
  2175.   ;; through end-of-line, et cetera.
  2176.   (interactive "P")
  2177.   (or comment-start-skip (error "No comment syntax defined"))
  2178.   (let ((count (prefix-numeric-value arg)) endc)
  2179.     (while (> count 0)
  2180.       (save-excursion
  2181.     (end-of-line)
  2182.     (setq endc (point))
  2183.     (beginning-of-line)
  2184.     (and (string< "" comment-end)
  2185.          (setq endc
  2186.            (progn
  2187.              (re-search-forward (regexp-quote comment-end) endc 'move)
  2188.              (skip-chars-forward " \t")
  2189.              (point))))
  2190.     (beginning-of-line)
  2191.     (if (re-search-forward comment-start-skip endc t)
  2192.         (progn
  2193.           (goto-char (match-beginning 0))
  2194.           (skip-chars-backward " \t")
  2195.           (kill-region (point) endc)
  2196.           ;; to catch comments a line beginnings
  2197.           (indent-according-to-mode))))
  2198.       (if arg (forward-line 1))
  2199.       (setq count (1- count)))))
  2200.  
  2201. (defun comment-region (beg end &optional arg)
  2202.   "Comment or uncomment each line in the region.
  2203. With just C-u prefix arg, uncomment each line in region.
  2204. Numeric prefix arg ARG means use ARG comment characters.
  2205. If ARG is negative, delete that many comment characters instead.
  2206. Comments are terminated on each line, even for syntax in which newline does
  2207. not end the comment.  Blank lines do not get comments."
  2208.   ;; if someone wants it to only put a comment-start at the beginning and
  2209.   ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
  2210.   ;; is easy enough.  No option is made here for other than commenting
  2211.   ;; every line.
  2212.   (interactive "r\nP")
  2213.   (or comment-start (error "No comment syntax is defined"))
  2214.   (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
  2215.   (save-excursion
  2216.     (save-restriction
  2217.       (let ((cs comment-start) (ce comment-end)
  2218.         numarg)
  2219.         (if (consp arg) (setq numarg t)
  2220.       (setq numarg (prefix-numeric-value arg))
  2221.       ;; For positive arg > 1, replicate the comment delims now,
  2222.       ;; then insert the replicated strings just once.
  2223.       (while (> numarg 1)
  2224.         (setq cs (concat cs comment-start)
  2225.           ce (concat ce comment-end))
  2226.         (setq numarg (1- numarg))))
  2227.     ;; Loop over all lines from BEG to END.
  2228.         (narrow-to-region beg end)
  2229.         (goto-char beg)
  2230.         (while (not (eobp))
  2231.           (if (or (eq numarg t) (< numarg 0))
  2232.           (progn
  2233.         ;; Delete comment start from beginning of line.
  2234.         (if (eq numarg t)
  2235.             (while (looking-at (regexp-quote cs))
  2236.               (delete-char (length cs)))
  2237.           (let ((count numarg))
  2238.             (while (and (> 1 (setq count (1+ count)))
  2239.                 (looking-at (regexp-quote cs)))
  2240.               (delete-char (length cs)))))
  2241.         ;; Delete comment end from end of line.
  2242.                 (if (string= "" ce)
  2243.             nil
  2244.           (if (eq numarg t)
  2245.               (progn
  2246.             (end-of-line)
  2247.             ;; This is questionable if comment-end ends in
  2248.             ;; whitespace.  That is pretty brain-damaged,
  2249.             ;; though.
  2250.             (skip-chars-backward " \t")
  2251.             (if (and (>= (- (point) (point-min)) (length ce))
  2252.                  (save-excursion
  2253.                    (backward-char (length ce))
  2254.                    (looking-at (regexp-quote ce))))
  2255.                 (delete-char (- (length ce)))))
  2256.             (let ((count numarg))
  2257.               (while (> 1 (setq count (1+ count)))
  2258.             (end-of-line)
  2259.             ;; this is questionable if comment-end ends in whitespace
  2260.             ;; that is pretty brain-damaged though
  2261.             (skip-chars-backward " \t")
  2262.             (save-excursion
  2263.               (backward-char (length ce))
  2264.               (if (looking-at (regexp-quote ce))
  2265.                   (delete-char (length ce))))))))
  2266.         (forward-line 1))
  2267.         ;; Insert at beginning and at end.
  2268.             (if (looking-at "[ \t]*$") ()
  2269.               (insert cs)
  2270.               (if (string= "" ce) ()
  2271.                 (end-of-line)
  2272.                 (insert ce)))
  2273.             (search-forward "\n" nil 'move)))))))
  2274.  
  2275. (defun backward-word (arg)
  2276.   "Move backward until encountering the end of a word.
  2277. With argument, do this that many times.
  2278. In programs, it is faster to call `forward-word' with negative arg."
  2279.   (interactive "p")
  2280.   (forward-word (- arg)))
  2281.  
  2282. (defun mark-word (arg)
  2283.   "Set mark arg words away from point."
  2284.   (interactive "p")
  2285.   (push-mark
  2286.     (save-excursion
  2287.       (forward-word arg)
  2288.       (point))
  2289.     nil t))
  2290.  
  2291. (defun kill-word (arg)
  2292.   "Kill characters forward until encountering the end of a word.
  2293. With argument, do this that many times."
  2294.   (interactive "p")
  2295.   (kill-region (point) (progn (forward-word arg) (point))))
  2296.  
  2297. (defun backward-kill-word (arg)
  2298.   "Kill characters backward until encountering the end of a word.
  2299. With argument, do this that many times."
  2300.   (interactive "p")
  2301.   (kill-word (- arg)))
  2302.  
  2303. (defun current-word (&optional strict)
  2304.   "Return the word point is on (or a nearby word) as a string.
  2305. If optional arg STRICT is non-nil, return nil unless point is within
  2306. or adjacent to a word."
  2307.   (save-excursion
  2308.     (let ((oldpoint (point)) (start (point)) (end (point)))
  2309.       (skip-syntax-backward "w_") (setq start (point))
  2310.       (goto-char oldpoint)
  2311.       (skip-syntax-forward "w_") (setq end (point))
  2312.       (if (and (eq start oldpoint) (eq end oldpoint))
  2313.       ;; Point is neither within nor adjacent to a word.
  2314.       (and (not strict)
  2315.            (progn
  2316.          ;; Look for preceding word in same line.
  2317.          (skip-syntax-backward "^w_"
  2318.                        (save-excursion (beginning-of-line)
  2319.                                (point)))
  2320.          (if (bolp)
  2321.              ;; No preceding word in same line.
  2322.              ;; Look for following word in same line.
  2323.              (progn
  2324.                (skip-syntax-forward "^w_"
  2325.                         (save-excursion (end-of-line)
  2326.                                 (point)))
  2327.                (setq start (point))
  2328.                (skip-syntax-forward "w_")
  2329.                (setq end (point)))
  2330.            (setq end (point))
  2331.            (skip-syntax-backward "w_")
  2332.            (setq start (point)))
  2333.          (buffer-substring start end)))
  2334.     (buffer-substring start end)))))
  2335.  
  2336. (defconst fill-prefix nil
  2337.   "*String for filling to insert at front of new line, or nil for none.
  2338. Setting this variable automatically makes it local to the current buffer.")
  2339. (make-variable-buffer-local 'fill-prefix)
  2340.  
  2341. (defconst auto-fill-inhibit-regexp nil
  2342.   "*Regexp to match lines which should not be auto-filled.")
  2343.  
  2344. ;; This function is the auto-fill-function of a buffer
  2345. ;; when Auto-Fill mode is enabled.
  2346. ;; It returns t if it really did any work.
  2347. (defun do-auto-fill ()
  2348.   (let (fc justify bol give-up
  2349.        (fill-prefix fill-prefix))
  2350.     (if (or (not (setq justify (current-justification)))
  2351.         (null (setq fc (current-fill-column)))
  2352.         (and (eq justify 'left)
  2353.          (<= (current-column) fc))
  2354.         (save-excursion (beginning-of-line) 
  2355.                 (setq bol (point))
  2356.                 (and auto-fill-inhibit-regexp
  2357.                  (looking-at auto-fill-inhibit-regexp))))
  2358.     nil ;; Auto-filling not required
  2359.       (if (memq justify '(full center right))
  2360.       (save-excursion (unjustify-current-line)))
  2361.  
  2362.       ;; Choose a fill-prefix automatically.
  2363.       (if (and adaptive-fill-mode
  2364.            (or (null fill-prefix) (string= fill-prefix "")))
  2365.       (let ((prefix
  2366.          (fill-context-prefix
  2367.           (save-excursion (backward-paragraph 1) (point))
  2368.           (save-excursion (forward-paragraph 1) (point))
  2369.           ;; Don't accept a non-whitespace fill prefix
  2370.           ;; from the first line of a paragraph.
  2371.           "^[ \t]*$")))
  2372.         (and prefix (not (equal prefix ""))
  2373.          (setq fill-prefix prefix))))
  2374.  
  2375.       (while (and (not give-up) (> (current-column) fc))
  2376.     ;; Determine where to split the line.
  2377.     (let ((fill-point
  2378.            (let ((opoint (point))
  2379.              bounce
  2380.              (first t))
  2381.          (save-excursion
  2382.            (move-to-column (1+ fc))
  2383.            ;; Move back to a word boundary.
  2384.            (while (or first
  2385.                   ;; If this is after period and a single space,
  2386.                   ;; move back once more--we don't want to break
  2387.                   ;; the line there and make it look like a
  2388.                   ;; sentence end.
  2389.                   (and (not (bobp))
  2390.                    (not bounce)
  2391.                    sentence-end-double-space
  2392.                    (save-excursion (forward-char -1)
  2393.                            (and (looking-at "\\. ")
  2394.                             (not (looking-at "\\.  "))))))
  2395.              (setq first nil)
  2396.              (skip-chars-backward "^ \t\n")
  2397.              ;; If we find nowhere on the line to break it,
  2398.              ;; break after one word.  Set bounce to t
  2399.              ;; so we will not keep going in this while loop.
  2400.              (if (bolp)
  2401.              (progn
  2402.                (re-search-forward "[ \t]" opoint t)
  2403.                (setq bounce t)))
  2404.              (skip-chars-backward " \t"))
  2405.            ;; Let fill-point be set to the place where we end up.
  2406.            (point)))))
  2407.       ;; If that place is not the beginning of the line,
  2408.       ;; break the line there.
  2409.       (if (save-excursion
  2410.         (goto-char fill-point)
  2411.         (not (bolp)))
  2412.           (let ((prev-column (current-column)))
  2413.         ;; If point is at the fill-point, do not `save-excursion'.
  2414.         ;; Otherwise, if a comment prefix or fill-prefix is inserted,
  2415.         ;; point will end up before it rather than after it.
  2416.         (if (save-excursion
  2417.               (skip-chars-backward " \t")
  2418.               (= (point) fill-point))
  2419.             (indent-new-comment-line t)
  2420.           (save-excursion
  2421.             (goto-char fill-point)
  2422.             (indent-new-comment-line t)))
  2423.         ;; Now do justification, if required
  2424.         (if (not (eq justify 'left))
  2425.             (save-excursion 
  2426.               (end-of-line 0)
  2427.               (justify-current-line justify nil t)))
  2428.         ;; If making the new line didn't reduce the hpos of
  2429.         ;; the end of the line, then give up now;
  2430.         ;; trying again will not help.
  2431.         (if (>= (current-column) prev-column)
  2432.             (setq give-up t)))
  2433.         ;; No place to break => stop trying.
  2434.         (setq give-up t))))
  2435.       ;; Justify last line.
  2436.       (justify-current-line justify t t)
  2437.       t))) 
  2438.  
  2439. (defvar normal-auto-fill-function 'do-auto-fill
  2440.   "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
  2441. Some major modes set this.")
  2442.  
  2443. (defun auto-fill-mode (&optional arg)
  2444.   "Toggle Auto Fill mode.
  2445. With arg, turn Auto Fill mode on if and only if arg is positive.
  2446. In Auto Fill mode, inserting a space at a column beyond `current-fill-column'
  2447. automatically breaks the line at a previous space.
  2448.  
  2449. The value of `normal-auto-fill-function' specifies the function to use
  2450. for `auto-fill-function' when turning Auto Fill mode on."
  2451.   (interactive "P")
  2452.   (prog1 (setq auto-fill-function
  2453.            (if (if (null arg)
  2454.                (not auto-fill-function)
  2455.                (> (prefix-numeric-value arg) 0))
  2456.            normal-auto-fill-function
  2457.            nil))
  2458.     (force-mode-line-update)))
  2459.  
  2460. ;; This holds a document string used to document auto-fill-mode.
  2461. (defun auto-fill-function ()
  2462.   "Automatically break line at a previous space, in insertion of text."
  2463.   nil)
  2464.  
  2465. (defun turn-on-auto-fill ()
  2466.   "Unconditionally turn on Auto Fill mode."
  2467.   (auto-fill-mode 1))
  2468.  
  2469. (defun set-fill-column (arg)
  2470.   "Set `fill-column' to specified argument.
  2471. Just \\[universal-argument] as argument means to use the current column."
  2472.   (interactive "P")
  2473.   (cond ((integerp arg)
  2474.      (setq fill-column arg))
  2475.     ((consp arg)
  2476.      (setq fill-column (current-column)))
  2477.     ;; Disallow missing argument; it's probably a typo for C-x C-f.
  2478.     (t
  2479.      (error "set-fill-column requires an explicit argument")))
  2480.   (message "fill-column set to %d" fill-column))
  2481.  
  2482. (defconst comment-multi-line nil
  2483.   "*Non-nil means \\[indent-new-comment-line] should continue same comment
  2484. on new line, with no new terminator or starter.
  2485. This is obsolete because you might as well use \\[newline-and-indent].")
  2486.  
  2487. (defun indent-new-comment-line (&optional soft)
  2488.   "Break line at point and indent, continuing comment if within one.
  2489. This indents the body of the continued comment
  2490. under the previous comment line.
  2491.  
  2492. This command is intended for styles where you write a comment per line,
  2493. starting a new comment (and terminating it if necessary) on each line.
  2494. If you want to continue one comment across several lines, use \\[newline-and-indent].
  2495.  
  2496. If a fill column is specified, it overrides the use of the comment column
  2497. or comment indentation.
  2498.  
  2499. The inserted newline is marked hard if `use-hard-newlines' is true, 
  2500. unless optional argument SOFT is non-nil."
  2501.   (interactive)
  2502.   (let (comcol comstart)
  2503.     (skip-chars-backward " \t")
  2504.     (delete-region (point)
  2505.            (progn (skip-chars-forward " \t")
  2506.               (point)))
  2507.     (if soft (insert-and-inherit ?\n) (newline 1))
  2508.     (if fill-prefix
  2509.     (progn
  2510.       (indent-to-left-margin)
  2511.       (insert-and-inherit fill-prefix))
  2512.       (if (not comment-multi-line)
  2513.       (save-excursion
  2514.         (if (and comment-start-skip
  2515.              (let ((opoint (point)))
  2516.                (forward-line -1)
  2517.                (re-search-forward comment-start-skip opoint t)))
  2518.         ;; The old line is a comment.
  2519.         ;; Set WIN to the pos of the comment-start.
  2520.         ;; But if the comment is empty, look at preceding lines
  2521.         ;; to find one that has a nonempty comment.
  2522.  
  2523.         ;; If comment-start-skip contains a \(...\) pair,
  2524.         ;; the real comment delimiter starts at the end of that pair.
  2525.         (let ((win (or (match-end 1) (match-beginning 0))))
  2526.           (while (and (eolp) (not (bobp))
  2527.                   (let (opoint)
  2528.                 (beginning-of-line)
  2529.                 (setq opoint (point))
  2530.                 (forward-line -1)
  2531.                 (re-search-forward comment-start-skip opoint t)))
  2532.             (setq win (or (match-end 1) (match-beginning 0))))
  2533.           ;; Indent this line like what we found.
  2534.           (goto-char win)
  2535.           (setq comcol (current-column))
  2536.           (setq comstart
  2537.             (buffer-substring (point) (match-end 0)))))))
  2538.       (if comcol
  2539.       (let ((comment-column comcol)
  2540.         (comment-start comstart)
  2541.         (comment-end comment-end))
  2542.         (and comment-end (not (equal comment-end ""))
  2543.   ;           (if (not comment-multi-line)
  2544.              (progn
  2545.                (forward-char -1)
  2546.                (insert comment-end)
  2547.                (forward-char 1))
  2548.   ;         (setq comment-column (+ comment-column (length comment-start))
  2549.   ;               comment-start "")
  2550.   ;           )
  2551.          )
  2552.         (if (not (eolp))
  2553.         (setq comment-end ""))
  2554.         (insert-and-inherit ?\n)
  2555.         (forward-char -1)
  2556.         (indent-for-comment)
  2557.         (save-excursion
  2558.           ;; Make sure we delete the newline inserted above.
  2559.           (end-of-line)
  2560.           (delete-char 1)))
  2561.     (indent-according-to-mode)))))
  2562.  
  2563. (defun set-selective-display (arg)
  2564.   "Set `selective-display' to ARG; clear it if no arg.
  2565. When the value of `selective-display' is a number > 0,
  2566. lines whose indentation is >= that value are not displayed.
  2567. The variable `selective-display' has a separate value for each buffer."
  2568.   (interactive "P")
  2569.   (if (eq selective-display t)
  2570.       (error "selective-display already in use for marked lines"))
  2571.   (let ((current-vpos
  2572.      (save-restriction
  2573.        (narrow-to-region (point-min) (point))
  2574.        (goto-char (window-start))
  2575.        (vertical-motion (window-height)))))
  2576.     (setq selective-display
  2577.       (and arg (prefix-numeric-value arg)))
  2578.     (recenter current-vpos))
  2579.   (set-window-start (selected-window) (window-start (selected-window)))
  2580.   (princ "selective-display set to " t)
  2581.   (prin1 selective-display t)
  2582.   (princ "." t))
  2583.  
  2584. (defconst overwrite-mode-textual " Ovwrt"
  2585.   "The string displayed in the mode line when in overwrite mode.")
  2586. (defconst overwrite-mode-binary " Bin Ovwrt"
  2587.   "The string displayed in the mode line when in binary overwrite mode.")
  2588.  
  2589. (defun overwrite-mode (arg)
  2590.   "Toggle overwrite mode.
  2591. With arg, turn overwrite mode on iff arg is positive.
  2592. In overwrite mode, printing characters typed in replace existing text
  2593. on a one-for-one basis, rather than pushing it to the right.  At the
  2594. end of a line, such characters extend the line.  Before a tab,
  2595. such characters insert until the tab is filled in.
  2596. \\[quoted-insert] still inserts characters in overwrite mode; this
  2597. is supposed to make it easier to insert characters when necessary."
  2598.   (interactive "P")
  2599.   (setq overwrite-mode
  2600.     (if (if (null arg) (not overwrite-mode)
  2601.           (> (prefix-numeric-value arg) 0))
  2602.         'overwrite-mode-textual))
  2603.   (force-mode-line-update))
  2604.  
  2605. (defun binary-overwrite-mode (arg)
  2606.   "Toggle binary overwrite mode.
  2607. With arg, turn binary overwrite mode on iff arg is positive.
  2608. In binary overwrite mode, printing characters typed in replace
  2609. existing text.  Newlines are not treated specially, so typing at the
  2610. end of a line joins the line to the next, with the typed character
  2611. between them.  Typing before a tab character simply replaces the tab
  2612. with the character typed.
  2613. \\[quoted-insert] replaces the text at the cursor, just as ordinary
  2614. typing characters do.
  2615.  
  2616. Note that binary overwrite mode is not its own minor mode; it is a
  2617. specialization of overwrite-mode, entered by setting the
  2618. `overwrite-mode' variable to `overwrite-mode-binary'."
  2619.   (interactive "P")
  2620.   (setq overwrite-mode
  2621.     (if (if (null arg)
  2622.         (not (eq overwrite-mode 'overwrite-mode-binary))
  2623.           (> (prefix-numeric-value arg) 0))
  2624.         'overwrite-mode-binary))
  2625.   (force-mode-line-update))
  2626.  
  2627. (defvar line-number-mode t
  2628.   "*Non-nil means display line number in mode line.")
  2629.  
  2630. (defun line-number-mode (arg)
  2631.   "Toggle Line Number mode.
  2632. With arg, turn Line Number mode on iff arg is positive.
  2633. When Line Number mode is enabled, the line number appears
  2634. in the mode line."
  2635.   (interactive "P")
  2636.   (setq line-number-mode
  2637.     (if (null arg) (not line-number-mode)
  2638.       (> (prefix-numeric-value arg) 0)))
  2639.   (force-mode-line-update))
  2640.  
  2641. (defvar column-number-mode nil
  2642.   "*Non-nil means display column number in mode line.")
  2643.  
  2644. (defun column-number-mode (arg)
  2645.   "Toggle Column Number mode.
  2646. With arg, turn Column Number mode on iff arg is positive.
  2647. When Column Number mode is enabled, the column number appears
  2648. in the mode line."
  2649.   (interactive "P")
  2650.   (setq column-number-mode
  2651.     (if (null arg) (not column-number-mode)
  2652.       (> (prefix-numeric-value arg) 0)))
  2653.   (force-mode-line-update))
  2654.  
  2655. (defvar blink-matching-paren t
  2656.   "*Non-nil means show matching open-paren when close-paren is inserted.")
  2657.  
  2658. (defvar blink-matching-paren-on-screen t
  2659.   "*Non-nil means show matching open-paren when it is on screen.
  2660. nil means don't show it (but the open-paren can still be shown
  2661. when it is off screen.")
  2662.  
  2663. (defconst blink-matching-paren-distance 12000
  2664.   "*If non-nil, is maximum distance to search for matching open-paren.")
  2665.  
  2666. (defconst blink-matching-delay 1
  2667.   "*The number of seconds that `blink-matching-open' will delay at a match.")
  2668.  
  2669. (defconst blink-matching-paren-dont-ignore-comments nil
  2670.   "*Non-nil means `blink-matching-paren' should not ignore comments.")
  2671.  
  2672. (defun blink-matching-open ()
  2673.   "Move cursor momentarily to the beginning of the sexp before point."
  2674.   (interactive)
  2675.   (and (> (point) (1+ (point-min)))
  2676.        blink-matching-paren
  2677.        ;; Verify an even number of quoting characters precede the close.
  2678.        (= 1 (logand 1 (- (point)
  2679.              (save-excursion
  2680.                (forward-char -1)
  2681.                (skip-syntax-backward "/\\")
  2682.                (point)))))
  2683.        (let* ((oldpos (point))
  2684.           (blinkpos)
  2685.           (mismatch))
  2686.      (save-excursion
  2687.        (save-restriction
  2688.          (if blink-matching-paren-distance
  2689.          (narrow-to-region (max (point-min)
  2690.                     (- (point) blink-matching-paren-distance))
  2691.                    oldpos))
  2692.          (condition-case ()
  2693.          (let ((parse-sexp-ignore-comments
  2694.             (and parse-sexp-ignore-comments
  2695.                  (not blink-matching-paren-dont-ignore-comments))))
  2696.            (setq blinkpos (scan-sexps oldpos -1)))
  2697.            (error nil)))
  2698.        (and blinkpos
  2699.         (/= (char-syntax (char-after blinkpos))
  2700.             ?\$)
  2701.         (setq mismatch
  2702.               (or (null (matching-paren (char-after blinkpos)))
  2703.               (/= (char-after (1- oldpos))
  2704.                   (matching-paren (char-after blinkpos))))))
  2705.        (if mismatch (setq blinkpos nil))
  2706.        (if blinkpos
  2707.            (progn
  2708.         (goto-char blinkpos)
  2709.         (if (pos-visible-in-window-p)
  2710.             (and blink-matching-paren-on-screen
  2711.              (sit-for blink-matching-delay))
  2712.           (goto-char blinkpos)
  2713.           (message
  2714.            "Matches %s"
  2715.            ;; Show what precedes the open in its line, if anything.
  2716.            (if (save-excursion
  2717.              (skip-chars-backward " \t")
  2718.              (not (bolp)))
  2719.                (buffer-substring (progn (beginning-of-line) (point))
  2720.                      (1+ blinkpos))
  2721.              ;; Show what follows the open in its line, if anything.
  2722.              (if (save-excursion
  2723.                (forward-char 1)
  2724.                (skip-chars-forward " \t")
  2725.                (not (eolp)))
  2726.              (buffer-substring blinkpos
  2727.                        (progn (end-of-line) (point)))
  2728.                ;; Otherwise show the previous nonblank line,
  2729.                ;; if there is one.
  2730.                (if (save-excursion
  2731.                  (skip-chars-backward "\n \t")
  2732.                  (not (bobp)))
  2733.                (concat
  2734.                 (buffer-substring (progn
  2735.                            (skip-chars-backward "\n \t")
  2736.                            (beginning-of-line)
  2737.                            (point))
  2738.                           (progn (end-of-line)
  2739.                              (skip-chars-backward " \t")
  2740.                              (point)))
  2741.                 ;; Replace the newline and other whitespace with `...'.
  2742.                 "..."
  2743.                 (buffer-substring blinkpos (1+ blinkpos)))
  2744.              ;; There is nothing to show except the char itself.
  2745.              (buffer-substring blinkpos (1+ blinkpos))))))))
  2746.          (cond (mismatch
  2747.             (message "Mismatched parentheses"))
  2748.            ((not blink-matching-paren-distance)
  2749.             (message "Unmatched parenthesis"))))))))
  2750.  
  2751. ;Turned off because it makes dbx bomb out.
  2752. (setq blink-paren-function 'blink-matching-open)
  2753.  
  2754. ;; This executes C-g typed while Emacs is waiting for a command.
  2755. ;; Quitting out of a program does not go through here;
  2756. ;; that happens in the QUIT macro at the C code level.
  2757. (defun keyboard-quit ()
  2758.   "Signal a  quit  condition.
  2759. During execution of Lisp code, this character causes a quit directly.
  2760. At top-level, as an editor command, this simply beeps."
  2761.   (interactive)
  2762.   (deactivate-mark)
  2763.   (signal 'quit nil))
  2764.  
  2765. (define-key global-map "\C-g" 'keyboard-quit)
  2766.  
  2767. (defvar buffer-quit-function nil
  2768.   "Function to call to \"quit\" the current buffer, or nil if none.
  2769. \\[keyboard-escape-quit] calls this function when its more local actions
  2770. \(such as cancelling a prefix argument, minibuffer or region) do not apply.")
  2771.  
  2772. (defun keyboard-escape-quit ()
  2773.   "Exit the current \"mode\" (in a generalized sense of the word).
  2774. This command can exit an interactive command such as `query-replace',
  2775. can clear out a prefix argument or a region,
  2776. can get out of the minibuffer or other recursive edit,
  2777. cancel the use of the current buffer (for special-purpose buffers),
  2778. or go back to just one window (by deleting all but the selected window)."
  2779.   (interactive)
  2780.   (cond ((eq last-command 'mode-exited) nil)
  2781.     ((> (minibuffer-depth) 0)
  2782.      (abort-recursive-edit))
  2783.     (current-prefix-arg
  2784.      nil)
  2785.     ((and transient-mark-mode
  2786.           mark-active)
  2787.      (deactivate-mark))
  2788.     (buffer-quit-function
  2789.      (funcall buffer-quit-function))
  2790.     ((not (one-window-p t))
  2791.      (delete-other-windows))))
  2792.  
  2793. (define-key global-map "\e\e\e" 'keyboard-escape-quit)
  2794.  
  2795. (defun set-variable (var val)
  2796.   "Set VARIABLE to VALUE.  VALUE is a Lisp object.
  2797. When using this interactively, supply a Lisp expression for VALUE.
  2798. If you want VALUE to be a string, you must surround it with doublequotes.
  2799.  
  2800. If VARIABLE has a `variable-interactive' property, that is used as if
  2801. it were the arg to `interactive' (which see) to interactively read the value."
  2802.   (interactive
  2803.    (let* ((var (read-variable "Set variable: "))
  2804.       (minibuffer-help-form
  2805.        '(funcall myhelp))
  2806.       (myhelp
  2807.        (function
  2808.         (lambda ()
  2809.           (with-output-to-temp-buffer "*Help*"
  2810.         (prin1 var)
  2811.         (princ "\nDocumentation:\n")
  2812.         (princ (substring (documentation-property var 'variable-documentation)
  2813.                   1))
  2814.         (if (boundp var)
  2815.             (let ((print-length 20))
  2816.               (princ "\n\nCurrent value: ")
  2817.               (prin1 (symbol-value var))))
  2818.         (save-excursion
  2819.           (set-buffer standard-output)
  2820.           (help-mode))
  2821.         nil)))))
  2822.      (list var
  2823.        (let ((prop (get var 'variable-interactive)))
  2824.          (if prop
  2825.          ;; Use VAR's `variable-interactive' property
  2826.          ;; as an interactive spec for prompting.
  2827.          (call-interactively (list 'lambda '(arg)
  2828.                        (list 'interactive prop)
  2829.                        'arg))
  2830.            (eval-minibuffer (format "Set %s to value: " var)))))))
  2831.   (set var val))
  2832.  
  2833. ;; Define the major mode for lists of completions.
  2834.  
  2835. (defvar completion-list-mode-map nil
  2836.   "Local map for completion list buffers.")
  2837. (or completion-list-mode-map
  2838.     (let ((map (make-sparse-keymap)))
  2839.       (define-key map [mouse-2] 'mouse-choose-completion)
  2840.       (define-key map [down-mouse-2] nil)
  2841.       (define-key map "\C-m" 'choose-completion)
  2842.       (define-key map "\e\e\e" 'delete-completion-window)
  2843.       (define-key map [left] 'previous-completion)
  2844.       (define-key map [right] 'next-completion)
  2845.       (setq completion-list-mode-map map)))
  2846.  
  2847. ;; Completion mode is suitable only for specially formatted data.
  2848. (put 'completion-list-mode 'mode-class 'special)
  2849.  
  2850. (defvar completion-reference-buffer nil
  2851.   "Record the buffer that was current when the completion list was requested.
  2852. This is a local variable in the completion list buffer.
  2853. Initial value is nil to avoid some compiler warnings.")
  2854.  
  2855. (defvar completion-base-size nil
  2856.   "Number of chars at beginning of minibuffer not involved in completion.
  2857. This is a local variable in the completion list buffer
  2858. but it talks about the buffer in `completion-reference-buffer'.
  2859. If this is nil, it means to compare text to determine which part
  2860. of the tail end of the buffer's text is involved in completion.")
  2861.  
  2862. (defun delete-completion-window ()
  2863.   "Delete the completion list window.
  2864. Go to the window from which completion was requested."
  2865.   (interactive)
  2866.   (let ((buf completion-reference-buffer))
  2867.     (delete-window (selected-window))
  2868.     (if (get-buffer-window buf)
  2869.     (select-window (get-buffer-window buf)))))
  2870.  
  2871. (defun previous-completion (n)
  2872.   "Move to the previous item in the completion list."
  2873.   (interactive "p")
  2874.   (next-completion (- n)))
  2875.  
  2876. (defun next-completion (n)
  2877.   "Move to the next item in the completion list.
  2878. With prefix argument N, move N items (negative N means move backward)."
  2879.   (interactive "p")
  2880.   (while (and (> n 0) (not (eobp)))
  2881.     (let ((prop (get-text-property (point) 'mouse-face))
  2882.       (end (point-max)))
  2883.       ;; If in a completion, move to the end of it.
  2884.       (if prop
  2885.       (goto-char (next-single-property-change (point) 'mouse-face nil end)))
  2886.       ;; Move to start of next one.
  2887.       (goto-char (next-single-property-change (point) 'mouse-face nil end)))
  2888.     (setq n (1- n)))
  2889.   (while (and (< n 0) (not (bobp)))
  2890.     (let ((prop (get-text-property (1- (point)) 'mouse-face))
  2891.       (end (point-min)))
  2892.       ;; If in a completion, move to the start of it.
  2893.       (if prop
  2894.       (goto-char (previous-single-property-change
  2895.               (point) 'mouse-face nil end)))
  2896.       ;; Move to end of the previous completion.
  2897.       (goto-char (previous-single-property-change (point) 'mouse-face nil end))
  2898.       ;; Move to the start of that one.
  2899.       (goto-char (previous-single-property-change (point) 'mouse-face nil end)))
  2900.     (setq n (1+ n))))
  2901.  
  2902. (defun choose-completion ()
  2903.   "Choose the completion that point is in or next to."
  2904.   (interactive)
  2905.   (let (beg end completion (buffer completion-reference-buffer)
  2906.     (base-size completion-base-size))
  2907.     (if (and (not (eobp)) (get-text-property (point) 'mouse-face))
  2908.     (setq end (point) beg (1+ (point))))
  2909.     (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face))
  2910.     (setq end (1- (point)) beg (point)))
  2911.     (if (null beg)
  2912.     (error "No completion here"))
  2913.     (setq beg (previous-single-property-change beg 'mouse-face))
  2914.     (setq end (or (next-single-property-change end 'mouse-face) (point-max)))
  2915.     (setq completion (buffer-substring beg end))
  2916.     (let ((owindow (selected-window)))
  2917.       (if (and (one-window-p t 'selected-frame)
  2918.            (window-dedicated-p (selected-window)))
  2919.       ;; This is a special buffer's frame
  2920.       (iconify-frame (selected-frame))
  2921.     (or (window-dedicated-p (selected-window))
  2922.         (bury-buffer)))
  2923.       (select-window owindow))
  2924.     (choose-completion-string completion buffer base-size)))
  2925.  
  2926. ;; Delete the longest partial match for STRING
  2927. ;; that can be found before POINT.
  2928. (defun choose-completion-delete-max-match (string)
  2929.   (let ((opoint (point))
  2930.     (len (min (length string)
  2931.           (- (point) (point-min)))))
  2932.     (goto-char (- (point) (length string)))
  2933.     (if completion-ignore-case
  2934.     (setq string (downcase string)))
  2935.     (while (and (> len 0)
  2936.         (let ((tail (buffer-substring (point)
  2937.                           (+ (point) len))))
  2938.           (if completion-ignore-case
  2939.               (setq tail (downcase tail)))
  2940.           (not (string= tail (substring string 0 len)))))
  2941.       (setq len (1- len))
  2942.       (forward-char 1))
  2943.     (delete-char len)))
  2944.  
  2945. ;; Switch to BUFFER and insert the completion choice CHOICE.
  2946. ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text
  2947. ;; to keep.  If it is nil, use choose-completion-delete-max-match instead.
  2948.  
  2949. ;; If BUFFER is the minibuffer, exit the minibuffer
  2950. ;; unless it is reading a file name and CHOICE is a directory.
  2951. (defun choose-completion-string (choice &optional buffer base-size)
  2952.   (let ((buffer (or buffer completion-reference-buffer)))
  2953.     ;; If BUFFER is a minibuffer, barf unless it's the currently
  2954.     ;; active minibuffer.
  2955.     (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
  2956.          (or (not (active-minibuffer-window))
  2957.          (not (equal buffer
  2958.                  (window-buffer (active-minibuffer-window))))))
  2959.     (error "Minibuffer is not active for completion")
  2960.       ;; Insert the completion into the buffer where completion was requested.
  2961.       (set-buffer buffer)
  2962.       (if base-size
  2963.       (delete-region (+ base-size (point-min)) (point))
  2964.     (choose-completion-delete-max-match choice))
  2965.       (insert choice)
  2966.       (remove-text-properties (- (point) (length choice)) (point)
  2967.                   '(mouse-face nil))
  2968.       ;; Update point in the window that BUFFER is showing in.
  2969.       (let ((window (get-buffer-window buffer t)))
  2970.     (set-window-point window (point)))
  2971.       ;; If completing for the minibuffer, exit it with this choice.
  2972.       (and (equal buffer (window-buffer (minibuffer-window)))
  2973.        minibuffer-completion-table
  2974.        ;; If this is reading a file name, and the file name chosen
  2975.        ;; is a directory, don't exit the minibuffer.
  2976.        (if (and (eq minibuffer-completion-table 'read-file-name-internal)
  2977.             (file-directory-p (buffer-string)))
  2978.            (select-window (active-minibuffer-window))
  2979.          (exit-minibuffer))))))
  2980.  
  2981. (defun completion-list-mode ()
  2982.   "Major mode for buffers showing lists of possible completions.
  2983. Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
  2984.  to select the completion near point.
  2985. Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
  2986.  with the mouse."
  2987.   (interactive)
  2988.   (kill-all-local-variables)
  2989.   (use-local-map completion-list-mode-map)
  2990.   (setq mode-name "Completion List")
  2991.   (setq major-mode 'completion-list-mode)
  2992.   (make-local-variable 'completion-base-size)
  2993.   (setq completion-base-size nil)
  2994.   (run-hooks 'completion-list-mode-hook))
  2995.  
  2996. (defvar completion-fixup-function nil
  2997.   "A function to customize how completions are identified in completion lists.
  2998. `completion-setup-function' calls this function with no arguments
  2999. each time it has found what it thinks is one completion.
  3000. Point is at the end of the completion in the completion list buffer.
  3001. If this function moves point, it can alter the end of that completion.")
  3002.  
  3003. ;; This function goes in completion-setup-hook, so that it is called
  3004. ;; after the text of the completion list buffer is written.
  3005.  
  3006. (defun completion-setup-function ()
  3007.   (save-excursion
  3008.     (let ((mainbuf (current-buffer)))
  3009.       (set-buffer standard-output)
  3010.       (completion-list-mode)
  3011.       (make-local-variable 'completion-reference-buffer)
  3012.       (setq completion-reference-buffer mainbuf)
  3013. ;;; The value 0 is right in most cases, but not for file name completion.
  3014. ;;; so this has to be turned off.
  3015. ;;;      (setq completion-base-size 0)
  3016.       (goto-char (point-min))
  3017.       (if window-system
  3018.       (insert (substitute-command-keys
  3019.            "Click \\[mouse-choose-completion] on a completion to select it.\n")))
  3020.       (insert (substitute-command-keys
  3021.            "In this buffer, type \\[choose-completion] to \
  3022. select the completion near point.\n\n"))
  3023.       (forward-line 1)
  3024.       (while (re-search-forward "[^ \t\n]+\\( [^ \t\n]+\\)*" nil t)
  3025.     (let ((beg (match-beginning 0))
  3026.           (end (point)))
  3027.       (if completion-fixup-function
  3028.           (funcall completion-fixup-function))
  3029.       (put-text-property beg (point) 'mouse-face 'highlight)
  3030.       (goto-char end))))))
  3031.  
  3032. (add-hook 'completion-setup-hook 'completion-setup-function)
  3033.  
  3034. (define-key minibuffer-local-completion-map [prior]
  3035.   'switch-to-completions)
  3036. (define-key minibuffer-local-must-match-map [prior]
  3037.   'switch-to-completions)
  3038. (define-key minibuffer-local-completion-map "\M-v"
  3039.   'switch-to-completions)
  3040. (define-key minibuffer-local-must-match-map "\M-v"
  3041.   'switch-to-completions)
  3042.  
  3043. (defun switch-to-completions ()
  3044.   "Select the completion list window."
  3045.   (interactive)
  3046.   ;; Make sure we have a completions window.
  3047.   (or (get-buffer-window "*Completions*")
  3048.       (minibuffer-completion-help))
  3049.   (select-window (get-buffer-window "*Completions*"))
  3050.   (goto-char (point-min))
  3051.   (search-forward "\n\n")
  3052.   (forward-line 1))
  3053.  
  3054. ;; Support keyboard commands to turn on various modifiers.
  3055.  
  3056. ;; These functions -- which are not commands -- each add one modifier
  3057. ;; to the following event.
  3058.  
  3059. (defun event-apply-alt-modifier (ignore-prompt)
  3060.   (vector (event-apply-modifier (read-event) 'alt 22 "A-")))
  3061. (defun event-apply-super-modifier (ignore-prompt)
  3062.   (vector (event-apply-modifier (read-event) 'super 23 "s-")))
  3063. (defun event-apply-hyper-modifier (ignore-prompt)
  3064.   (vector (event-apply-modifier (read-event) 'hyper 24 "H-")))
  3065. (defun event-apply-shift-modifier (ignore-prompt)
  3066.   (vector (event-apply-modifier (read-event) 'shift 25 "S-")))
  3067. (defun event-apply-control-modifier (ignore-prompt)
  3068.   (vector (event-apply-modifier (read-event) 'control 26 "C-")))
  3069. (defun event-apply-meta-modifier (ignore-prompt)
  3070.   (vector (event-apply-modifier (read-event) 'meta 27 "M-")))
  3071.  
  3072. (defun event-apply-modifier (event symbol lshiftby prefix)
  3073.   "Apply a modifier flag to event EVENT.
  3074. SYMBOL is the name of this modifier, as a symbol.
  3075. LSHIFTBY is the numeric value of this modifier, in keyboard events.
  3076. PREFIX is the string that represents this modifier in an event type symbol."
  3077.   (if (numberp event)
  3078.       (cond ((eq symbol 'control)
  3079.          (if (and (<= (downcase event) ?z)
  3080.               (>= (downcase event) ?a))
  3081.          (- (downcase event) ?a -1)
  3082.            (if (and (<= (downcase event) ?Z)
  3083.             (>= (downcase event) ?A))
  3084.            (- (downcase event) ?A -1)
  3085.          (logior (lsh 1 lshiftby) event))))
  3086.         ((eq symbol 'shift)
  3087.          (if (and (<= (downcase event) ?z)
  3088.               (>= (downcase event) ?a))
  3089.          (upcase event)
  3090.            (logior (lsh 1 lshiftby) event)))
  3091.         (t
  3092.          (logior (lsh 1 lshiftby) event)))
  3093.     (if (memq symbol (event-modifiers event))
  3094.     event
  3095.       (let ((event-type (if (symbolp event) event (car event))))
  3096.     (setq event-type (intern (concat prefix (symbol-name event-type))))
  3097.     (if (symbolp event)
  3098.         event-type
  3099.       (cons event-type (cdr event)))))))
  3100.  
  3101. (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier)
  3102. (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier)
  3103. (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier)
  3104. (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier)
  3105. (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier)
  3106. (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier)
  3107.  
  3108. ;;;; Keypad support.
  3109.  
  3110. ;;; Make the keypad keys act like ordinary typing keys.  If people add
  3111. ;;; bindings for the function key symbols, then those bindings will
  3112. ;;; override these, so this shouldn't interfere with any existing
  3113. ;;; bindings.
  3114.  
  3115. ;; Also tell read-char how to handle these keys.
  3116. (mapcar
  3117.  (lambda (keypad-normal)
  3118.    (let ((keypad (nth 0 keypad-normal))
  3119.      (normal (nth 1 keypad-normal)))
  3120.      (put keypad 'ascii-character normal)
  3121.      (define-key function-key-map (vector keypad) (vector normal))))
  3122.  '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4)
  3123.    (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9)
  3124.    (kp-space ?\ )
  3125.    (kp-tab ?\t)
  3126.    (kp-enter ?\r)
  3127.    (kp-multiply ?*)
  3128.    (kp-add ?+)
  3129.    (kp-separator ?,)
  3130.    (kp-subtract ?-)
  3131.    (kp-decimal ?.)
  3132.    (kp-divide ?/)
  3133.    (kp-equal ?=)))
  3134.  
  3135. ;;; simple.el ends here
  3136.